wxPython:为什么面板在运行时缺少 TextCtrl 和 Button?

问题描述 投票:0回答:0

我有一个简单的密码生成器:

import wx, string, secrets

def main():
    
    app = wx.App()
    
    class PwdGenFrame(wx.Frame):
        
        pnl = wx.Panel()
        
        def __init__(self, *args, **kw):
            super().__init__(*args, **kw)
            self.makeMenuBar()
            self.CreateStatusBar()
            self.SetStatusText('Version 0.10')
                
        def makeMenuBar(self):
            fileMenu = wx.Menu()
            exitItem = fileMenu.Append(wx.ID_EXIT)
            menuBar = wx.MenuBar()
            self.SetMenuBar(menuBar)
            self.Bind(wx.EVT_MENU, self.OnExit, exitItem)
            
            menuBar.Append(fileMenu, "&File")
        
        def OnExit(self, event):
            self.Close(True)

        pwd_length_input = wx.TextCtrl(pnl, -1, 'Enter the length of the password you would like')
        pwd_length_input.SetInitialSize(pwd_length_input.GetSizeFromTextSize(pwd_length_input.GetTextExtent('Enter the length of the password you would like')))
        button = wx.Button(pnl, -1, 'Generate')
        frame_output = wx.TextCtrl(pnl, -1, '', style=wx.TE_READONLY)
        
        def gen_pwd(self, event):
            lower_case = string.ascii_lowercase; upper_case = string.ascii_uppercase; numbers = string.digits; characters = string.punctuation; 
            concoctaneted_list = lower_case + upper_case + numbers + characters
            password_length = self.pwd_length_input.GetValue()
            
            while True:
                generated_password = ''
                try:
                    if password_length < 8:
                        print('Minimum password length is 8 characters.')
                        password_length = int(input('Enter the length of the password you would like: '))
                finally:
                    for i in range(password_length):
                        generated_password += ''.join(secrets.choice(concoctaneted_list))
                        
                    if (sum(letter in lower_case for letter in generated_password)>=2) and (sum(letter in upper_case for letter in generated_password)>=2) and (sum(num in numbers for num in generated_password)>=2) and (sum(c in characters for c in generated_password)>=2):
                            break
                
                self.frame_output.SetValue(generated_password)
        
        button.Bind(wx.EVT_BUTTON, gen_pwd)

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(pwd_length_input, 0, wx.EXPAND|wx.ALL, 10)
        sizer.Add(button, 0, wx.EXPAND|wx.ALL, 10)
        sizer.Add(frame_output, 0, wx.EXPAND|wx.ALL, 10)
        pnl.SetSizer(sizer)
    
    frm = PwdGenFrame(None, title='Password Generator', size=wx.Size(600, 300))
    frm.Show(); frm.CenterOnScreen(); app.MainLoop()


if __name__ == '__main__':
    main()

当我运行这个程序时,这个窗口出现: Empty Panel

预期的是2个

wx.TextCtrl
盒子和盒子之间的
wx.Button
按钮。但我不确定为什么它不起作用。

documentation

wx.Panel(self)
出现在
__init__
方法中,但是当我尝试这样做时,我无法分配
wx.TextCtrl
wx.Button

python wxpython
© www.soinside.com 2019 - 2024. All rights reserved.