【问题标题】:wxPython Dialog freezes after ShowModalwxPython 对话框在 ShowModal 后冻结
【发布时间】:2013-05-16 14:30:24
【问题描述】:

直接上代码,千言万语。

#!/usr/bin/env python2

import wx

class TestDialog(wx.Dialog):
    def __init__self(*arg, **args):
        wx.Dialog.__init__(self, parent, id, title, size=(350,300))
        sizer = self.CreateTextSizer('My Buttons')
        # bad()
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(wx.Button(self, -1, 'Button'), 0, wx.ALL, 5)
        sizer.Add(wx.Button(self, -1, 'Button'), 0, wx.ALL, 5)
        sizer.Add(wx.Button(self, -1, 'Button'), 0, wx.ALL, 5)
        sizer.Add(wx.Button(self, -1, 'Button'), 0, wx.ALL|wx.ALIGN_CENTER, 5)
        sizer.Add(wx.Button(self, -1, 'Button'), 0, wx.ALL|wx.EXPAND, 5)
        sizer.Fit(self)
        self.SetSizer(sizer)

    def InitUI(self):
        pnl = wx.Panel(self)
        vbox = wx.BoxSizer(wx.VERTICAL)
        btn = wx.Button(pnl, label='Ok')
        vbox.Add(btn, 1, flag=wx.LEFT)
        pnl.SetSizer(vbox)

    def OnClose(self):
        self.Destroy()


class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(550,500))

        panel = wx.Panel(self, -1)
        wx.Button(panel, 1, 'Show Custom Dialog', (100,100))
        self.Bind (wx.EVT_BUTTON, self.OnShowCustomDialog)

    def OnShowCustomDialog(self, event):
        dia = TestDialog(self, -1, 'buttons')
        dia.ShowModal()
        # dia.Destroy()

if __name__ == "__main__":
    try:
        app = wx.App()
        frame = MyFrame(None, wx.ID_ANY, 'dialog')
        frame.Show()
        # import wx.lib.inspection
        # wx.lib.inspection.InspectionTool().Show()
        app.MainLoop()
    except:
        import sys
        import traceback
        xc = traceback.format_exception(*sys.exc_info())
        wx.MessageBox(''.join(xc))

显示主窗口,但显示对话框后似乎没有执行任何操作。即使取消注释 bad() 调用似乎也没有显示任何内容。

【问题讨论】:

    标签: python python-2.7 wxpython


    【解决方案1】:

    改变这个:

    class TestDialog(wx.Dialog):
        def __init__self(*arg, **args):
    

    到这里:

    class TestDialog(wx.Dialog):
        def __init__(self, parent, id, title)
    

    我猜这是你的错字。把它改成上面的样子,你就得到你想要的结果了。创建实例时,类的__init__ 方法将自动运行。除非显式调用,否则 __init__self 方法将永远不会运行。

    【讨论】:

    • 任何提示您是如何发现它的?这是我一天的结束,所以盯着这个对我没有帮助:)
    • 测试了代码,直到发现错误。当问题包含将运行的代码时,总是很好。不同的眼睛可能会有很大帮助。
    • 你也许能帮我解决我的problem
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 2023-03-15
    • 2014-01-17
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多