簡體   English   中英

為什么使用self.Destroy()而不是self.Close()時,此wxPython GUI凍結? (示例附后)

[英]Why does this wxPython GUI freeze when self.Destroy() is used instead of self.Close()? (example attached)

LoginDialogself.loginButton打開,關閉它時,GUI凍結; 登錄按鈕保持按下狀態,並且在嘗試單擊任何內容時僅發出警報聲。

LoginDialog()來自本教程 ,只是在onLogin添加了文件寫入行(這不是問題源)。 從self.Destroy()切換到self.Close()時,一切似乎都可以正常工作。

wxPython版本是3.0,Python 2.7.10

以下代碼是該問題的有效示例:

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那個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM