try:
import wx
except ImportError:
print 'Module not found'
class Frame(wx.Frame):
def __init__(parent,id ):
wx.Frame.__init__(self,parent,id,)
panel = wx.Panel(self)
button = wx.Button(panel,label = 'close',size = (50,50))
self.Bind(wx.EVT_BUTTON,self.OnCloseMe,button)
self.Bind(wx.EVT_CLOSE,self.OnCloseWindow)
def OnCloseMe(self,event):
self.Close(True)
def OncloseWindow(self,event):
self.Destroy()
if __name__ == '__main__':
app = wx.App()
frame = wx.Frame(parent = None, id =-1,title = 'Widget',size = (300,100))
frame.Show()
app.MainLoop()
大家好,上面的代码只是在wxpython中创建一个按钮。但每次运行代码时,只显示Frame,没有任何按钮或内部任何内容,只有blank.i尝试重新安装wxpython模块但没有运气。 我的第二个问题是每当我尝试在框架构造函数中初始化say,title ='widget',size =(300,100),如wx.Frame。 init (self,parent,id,title ='widget ',size =(300,100)它不起作用,我必须通过这一行: frame = wx.Frame(parent = None,id = -1,title ='Widget',size =(300,100)) 为什么会那么。谢谢
答案 0 :(得分:0)
这是一个工作代码,只需添加您的事件即可。还可以考虑使用wxGlade以图形方式设计您的gui而不是编写它:
import wx
import gettext
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.panel_1 = wx.Panel(self, wx.ID_ANY)
self.button_1 = wx.Button(self.panel_1, wx.ID_ANY, _("button_1"))
self.__set_properties()
self.__do_layout()
def __set_properties(self):
self.SetTitle(_("frame_1"))
def __do_layout(self):
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
sizer_2.Add(self.button_1, 0, 0, 0)
self.panel_1.SetSizer(sizer_2)
sizer_1.Add(self.panel_1, 1, wx.EXPAND, 0)
self.SetSizer(sizer_1)
sizer_1.Fit(self)
self.Layout()
if __name__ == "__main__":
gettext.install("app")
app = wx.PySimpleApp(0)
wx.InitAllImageHandlers()
frame_1 = MyFrame(None, wx.ID_ANY, "")
app.SetTopWindow(frame_1)
frame_1.Show()
app.MainLoop()