关于wx.Dialog的基本问题

时间:2017-05-14 19:33:24

标签: wxpython

我想用wx.Dialog创建一个对话框。

我有两个问题。

我是否必须将wx.Frame设置为父窗口,还是可以将wx.Dialog用作主窗口?

Sizer在没有父级的wx.Dialog中是否可用?

感谢您的回答。

2 个答案:

答案 0 :(得分:1)

顺序为否,您不必设置wx.Frame
是的,您可以单独使用Dialog 是的,可以在对话框中使用sizer 这是一个例子:

#!/usr/bin/env python
import wx
class TestDialog(wx.Dialog):
    def __init__(self, parent, msg, title):
        wx.Dialog.__init__(self, parent, id=-1, title=title)
        Buttons = []
        Buttons.append(wx.Button(self,1, "Approve Location"))
        Buttons.append(wx.Button(self,2, "Approve Item"))
        Buttons.append(wx.Button(self,3, "Change Qty"))
        Buttons.append(wx.Button(self,4, "Approve"))
        sizer = wx.GridBagSizer(5,3)
        sizer.Add(Buttons[0], (0, 5), (1,1), wx.EXPAND)
        sizer.Add(Buttons[1], (1, 4), (1,1), wx.EXPAND)
        sizer.Add(Buttons[2], (1, 5), (1,1), wx.EXPAND)
        sizer.Add(Buttons[3], (2, 5), (1,1), wx.EXPAND)
        self.Bind(wx.EVT_BUTTON, self.OnLocation, id=1)
        self.Bind(wx.EVT_BUTTON, self.OnItem, id=2)
        self.Bind(wx.EVT_BUTTON, self.OnQty, id=3)
        self.Bind(wx.EVT_BUTTON, self.OnApprove, id=4)
        self.buttonpressed = None
        self.SetSizerAndFit(sizer)
        self.Centre()
    def OnLocation(self,event):
        self.EndModal(1)
        self.buttonpressed="Location"
    def OnItem(self,event):
        self.EndModal(2)
        self.buttonpressed="Item"
    def OnQty(self,event):
        self.EndModal(3)
        self.buttonpressed="Qty"
    def OnApprove(self,event):
        self.EndModal(4)
        self.buttonpressed="Approve"

if __name__ == "__main__":
    app = wx.App()
    dlg = TestDialog(None, "test my dialog", "Test Title")
    val = dlg.ShowModal()
    print "Dialog numeric result: " + str(val)
    print "Dialog text: " + str(dlg.buttonpressed)

答案 1 :(得分:0)

是的,你可以。将None作为父级传递。这应该对sizer行为没有影响。请务必在关闭后销毁对话框,以防止出现孤立对话框。