为什么使用self.Destroy()而不是self.Close()时,此wxPython GUI冻结? (示例附后) [英] Why does this wxPython GUI freeze when self.Destroy() is used instead of self.Close()? (example attached)
问题描述
LoginDialog
用self.loginButton
打开,并且在关闭时,GUI冻结;登录按钮将保持按下状态,并且在尝试单击任何内容时仅会发出警报声.
LoginDialog
is opened with self.loginButton
, and upon closing it, the GUI freezes; the login button remains pressed, and there are only alert sounds when attempting to click on anything.
LoginDialog()
was from this tutorial, just augmented with the file-write line in onLogin
(which isn't the problem source). All appears to work when switching from self.Destroy() to self.Close().
wxPython版本为3.0,Python 2.7.10
wxPython version is 3.0, Python 2.7.10
以下代码是该问题的有效示例:
The following code is a working example of the issue:
import wx
# wxGlade dependency
import gettext
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
wx.Frame.__init__(self, *args, **kwds)
self.updateLoginButton = wx.Button(self, wx.ID_ANY, _("Store/Update Login"))
self.Bind(wx.EVT_BUTTON, self.updateLogin, self.updateLoginButton)
self.__set_properties()
self.__do_layout()
def __set_properties(self):
self.SetTitle(_("Dialog Test"))
def __do_layout(self):
sizer_1 = wx.BoxSizer(wx.VERTICAL)
grid_sizer_1 = wx.FlexGridSizer(3, 1, 1, 0)
exportButtons_sizer = wx.FlexGridSizer(1, 1, 1, 10)
exportButtons_sizer.Add(self.updateLoginButton, 0, wx.TOP | wx.ALIGN_CENTER, 15)
grid_sizer_1.Add(exportButtons_sizer, 0, wx.TOP | wx.ALIGN_CENTER, 20)
sizer_1.Add(grid_sizer_1, 1, wx.ALL | wx.EXPAND, 15)
self.SetSizer(sizer_1)
sizer_1.Fit(self)
self.Layout()
def updateLogin(self, event):
dlg = LoginDialog(self, -1)
dlg.ShowModal()
class MyApp(wx.App):
def OnInit(self):
frame_1 = MyFrame(None, wx.ID_ANY, "")
self.SetTopWindow(frame_1)
frame_1.Show()
return True
class LoginDialog(wx.Dialog):
"""
Class to define login dialog
"""
# ----------------------------------------------------------------------
def __init__(self, parent, id, title="Update Login Info"):
"""Constructor"""
wx.Dialog.__init__(self, parent, id, title)
# user info
user_sizer = wx.BoxSizer(wx.HORIZONTAL)
user_lbl = wx.StaticText(self, label="Username:")
user_sizer.Add(user_lbl, 0, wx.ALL | wx.CENTER, 5)
self.user = wx.TextCtrl(self)
user_sizer.Add(self.user, 0, wx.ALL, 5)
# pass info
p_sizer = wx.BoxSizer(wx.HORIZONTAL)
p_lbl = wx.StaticText(self, label="Password:")
p_sizer.Add(p_lbl, 0, wx.ALL | wx.CENTER, 5)
self.password = wx.TextCtrl(self, style=wx.TE_PASSWORD | wx.TE_PROCESS_ENTER)
p_sizer.Add(self.password, 0, wx.ALL, 5)
main_sizer = wx.BoxSizer(wx.VERTICAL)
main_sizer.Add(user_sizer, 0, wx.ALL, 5)
main_sizer.Add(p_sizer, 0, wx.ALL, 5)
btn = wx.Button(self, label="OK")
btn.Bind(wx.EVT_BUTTON, self.onLogin)
main_sizer.Add(btn, 0, wx.ALL | wx.CENTER, 5)
self.SetSizer(main_sizer)
self.__set_properties()
# ----------------------------------------------------------------------
def onLogin(self, event):
"""
Check credentials and login
"""
password = self.password.GetValue()
email = self.user.GetValue()
with open('login.txt', 'w') as f:
f.write(email + ', ' + password)
self.Destroy()
def __set_properties(self):
self.user.SetMinSize((220, 20))
if __name__ == "__main__":
gettext.install("app")
app = MyApp(0)
app.MainLoop()
推荐答案
如果打算将一个对话框与ShowModal
对话框一起使用,则该对话框完成后应调用EndModal
,而调用方应为如果需要,谁会调用Destroy
从对话框中获取值之后.
If a dialog is intended to be used with ShowModal
dialog then the dialog should call EndModal
when it is done, and the caller should be the one who calls Destroy
after it has fetched values from the dialog if needed.
这篇关于为什么使用self.Destroy()而不是self.Close()时,此wxPython GUI冻结? (示例附后)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!