关于 wx.Dialog 的基本问题
Basic questions about wx.Dialog
我想用 wx.Dialog 创建一个对话框。
我有两个问题。
我必须将 wx.Frame 设置为 parent window 还是可以将我的 wx.Dialog 用作主要 window?
在 wx.Dialog 没有 parent 的情况下,Sizers 是否可用?
感谢您的回答。
是的,你可以。将 None
作为父项传递。这应该不会影响 sizer 的行为。请务必在对话框关闭后将其销毁,以防止出现孤立的对话框。
为了不,你不必设置wx.Frame
是的,您可以单独使用对话框
是的,可以在对话框中使用 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)
我想用 wx.Dialog 创建一个对话框。
我有两个问题。
我必须将 wx.Frame 设置为 parent window 还是可以将我的 wx.Dialog 用作主要 window?
在 wx.Dialog 没有 parent 的情况下,Sizers 是否可用?
感谢您的回答。
是的,你可以。将 None
作为父项传递。这应该不会影响 sizer 的行为。请务必在对话框关闭后将其销毁,以防止出现孤立的对话框。
为了不,你不必设置wx.Frame
是的,您可以单独使用对话框
是的,可以在对话框中使用 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)