简体   繁体   English

wxPython自定义对话框加载不正确

[英]wxPython Custom Dialog loading incorrectly

I am trying to create a custom dialog to display information. 我正在尝试创建一个自定义对话框来显示信息。 It is activated on a button press, and that mechanism is working perfectly. 按下按钮即可激活它,并且该机制运行良好。 However, the dialog itself is broken. 但是,对话框本身已损坏。 Any help is appreciated. 任何帮助表示赞赏。

import wx

class ForgotPass(wx.Dialog):

    def __init__(self, *args, **kwargs):
         super(ForgotPass, self).__init__(*args, **kwargs) 

        self.InitUI()

    def InitUI(self):
        self.panel = wx.Panel(self)
        self.sizer = wx.BoxSizer(wx.VERTICAL)

        self.title = wx.StaticText(self.panel, label='Forgotten Your Password?', style=wx.ALIGN_CENTER)
        self.title.SetFont(wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.BOLD, underline=True))  # Underlined & Bold
        self.sizer.Add(self.title)

        self.text = wx.StaticText(self.panel, label="Contact 'TheHiguty' via ingame PM to have your password reset!", style=wx.ALIGN_CENTER)
        self.sizer.Add(self.text)

        self.SetSizer(self.sizer)
        self.SetSize((200, 150))
        self.SetTitle('Forgotten Your Password')
        self.Center()
        self.Show(True)

def main():
    app = wx.App(False)
    ForgotPass(None)
    app.MainLoop()

if __name__ == "__main__":
    main()

However rather than displaying the text correctly, I get this: 但是,而不是正确显示文本,我得到了:

在此处输入图片说明

Any help to fix this issue is greatly appreciated! 非常感谢您解决此问题的任何帮助!

Add self.sizer.Fit(self.panel) after self.SetSizer(self.sizer) to fix the problem. 加入self.sizer.Fit(self.panel)self.SetSizer(self.sizer)来解决这个问题。 Although, if you do so, you'll find that the horizontal width is too small, so you might want to expand that. 尽管这样做会发现水平宽度太小,所以您可能需要扩展它。 Complete code with fix: 带有修复的完整代码:

import wx

class ForgotPass(wx.Dialog):

    def __init__(self, *args, **kwargs):
        super(ForgotPass, self).__init__(*args, **kwargs) 

        self.InitUI()

    def InitUI(self):
        self.panel = wx.Panel(self)
        self.sizer = wx.BoxSizer(wx.VERTICAL)

        self.title = wx.StaticText(self.panel, label='Forgotten Your Password?', style=wx.ALIGN_CENTER)
        self.title.SetFont(wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.BOLD, underline=True))  # Underlined & Bold
        self.sizer.Add(self.title)

        self.text = wx.StaticText(self.panel, label="Contact 'TheHiguty' via ingame PM to have your password reset!", style=wx.ALIGN_CENTER)
        self.sizer.Add(self.text)

        self.SetSizer(self.sizer)
        self.sizer.Fit(self.panel)
        self.SetSize((400, 150))
        self.SetTitle('Forgotten Your Password')
        self.Center()
        self.Show(True)

def main():
    app = wx.App(False)
    ForgotPass(None)
    app.MainLoop()

if __name__ == "__main__":
    main()

Although, there is a way to create dialogs that look cleaner and looks more native to whatever OS the program is run on: use wx.MessageBox 虽然,有一种方法可以创建对话框,使对话框看起来更简洁,并且对于程序运行所在的任何操作系统而言,本机看起来都更原生:使用wx.MessageBox

Here's a simple example: 这是一个简单的例子:

import wx

class MainWindow(wx.Frame):
    def __init__(self, parent):
        super(MainWindow, self).__init__(parent)

        self.sizer = wx.BoxSizer(wx.VERTICAL)

        self.button = wx.Button(self, wx.ID_ANY, label="Test")
        self.sizer.Add(self.button)
        self.Bind(wx.EVT_BUTTON, self.password_dialog, self.button)

        self.SetSizer(self.sizer)
        self.sizer.Fit(self)
        self.SetTitle('Test')
        self.SetSize((100, 100))
        self.Centre()
        self.Show(True)

    def password_dialog(self, event):
        wx.MessageBox(
            'Contact `TheHiguty` blah blah blah',
            'Forgotten your password?',
            wx.OK|wx.ICON_ERROR)

if __name__ == '__main__':
    app = wx.App()
    MainWindow(None)
    app.MainLoop()

I understand that this is an older question, but I would like to note what I use for custom dialog boxes and do not have the "noise / icon" (at least in my opinion). 我知道这是一个较旧的问题,但是我想指出一下我用于自定义对话框的内容,并且没有“噪音/图标”(至少在我看来)。 This is working with wxPython 2.9.5.0. 这适用于wxPython 2.9.5.0。

This is the code I use: 这是我使用的代码:

from wx.lib.pubsub import pub

def showConfirmDlg(message, caption, flag=wx.OK|wx.ICON_NONE):
    msg = wx.MessageDialog(None, message=message,
                       caption=caption, style=flag)
    msg.ShowModal()
    msg.Destroy()

pub.subscribe(showConfirmDlg, 'dialog.confirm')

As you can see in the snippet I can use this function repeatedly for various messages and eliminate the need for multiple function definitions. 如您在代码段中所见,我可以针对各种消息重复使用此函数,并且不需要多个函数定义。 To show the dialog box with the text in your example I would do this: 要在您的示例中显示带有文本的对话框,我可以这样做:

def onButtonPress(self, event):
    pub.sendMessage('dialog.confirm',
        message="Forgot your password?\n Contact 'TheHiguty' (rest of text)",
        caption="Forgot Password?") # Could even just put caption=""

For further re-usability options you can replace the "flag=wx.OK|wx.ICON_NONE" with just "flag" and add "flag=wx.OK|(whatever style)" to your function call. 对于进一步的可重用性选项,您可以仅用“ flag”替换“ flag = wx.OK | wx.ICON_NONE”,并在函数调用中添加“ flag = wx.OK |(任何样式)”。

For more examples regarding Python and wxPython I highly recommend Mike Driscoll's blog site The Mouse vs The Python. 有关Python和wxPython的更多示例,我强烈推荐Mike Driscoll的博客网站The Mouse vs The Python。 I have found it amazingly informative and have learned a lot about wxPython from Mr Driscoll. 我发现它提供了惊人的信息,并从Driscoll先生那里学到了很多有关wxPython的知识。

Hope this helps you and others! 希望这对您和其他人有帮助!

--EDIT-- I should mention that I found wx.ICON_NONE by accident while trying to remove icons from my message boxes myself. -编辑-我应该提一下,我自己尝试从消息框中删除图标时偶然发现了wx.ICON_NONE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM