1 min read

3초 후에 닫히는 wxPython 다이얼로그 상자

3초 후에 닫히는 wxPython 다이얼로그 상자
Photo by Hitesh Choudhary / Unsplash

열심히 구글링해서 짜맞춘 시간 한정 다이얼로그 상자 코드.
ShowModal() 함수로 화면에 나타난 창을 일정 시간이 흐른 후 닫히게 했다.

import wx
import os, sys

class MyDialog(wx.Dialog):
  def __init__(self, parent, id, title, msg):
    wx.Dialog.__init__(self, parent, id, title,
      size=(360,300),
      style=wx.STAY_ON_TOP)
    self._timeout = 3 # sec.
    label = wx.StaticText(self, -1, msg)

  def ShowModal(self):
    self._startTimer()
    self.MakeModal(True)
    wx.Dialog.ShowModal(self)

  def _startTimer(self):
    if self._timeout > 0:
      self._timer = wx.Timer(self)
      self._timer.Start(self._timeout*1000, False) # msec.
      self.Bind(wx.EVT_TIMER, self._onTimeout, self._timer)

  def _onTimeout(self, evt):
    wx.Dialog.EndModal(self, wx.ID_OK)
# End of class MyDialog

app = wx.PySimpleApp()
dlg = MyDialog(None, -1, 'title', 'message')
dlg.ShowModal()
dlg.Destroy()
app.MainLoop()
  • MyDialogself._timeout 값으로 다이얼로그 상자가 닫히는 시간을 조절할 수 있다.
  • wx.STAY_ON_TOP은 다른 창들 보다 위에 다이얼로그 상자가 나오도록 하기 위해 필요.
  • 당연한 이야기지만 Python 및 wxPython을 미리 설치해야 한다.
— END OF POST.