一个如下图所示的简单GUI界面
实现的功能也非常简单,在主窗口输入字符,点击复制就在副窗口出现相同的字符。点击清空同时清空主副窗口内的字符。
代码如下:
首先是写GUI界面
import wx import wx.xrc class test_0(wx.Frame): def __init__(self, parent): wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=wx.EmptyString, pos=wx.DefaultPosition, size=wx.Size(400, 250), style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL) self.SetSizeHintsSz(wx.DefaultSize, wx.DefaultSize) bSizer1 = wx.BoxSizer(wx.VERTICAL) self.m_staticText1 = wx.StaticText(self, wx.ID_ANY, u"主窗口", wx.DefaultPosition, wx.DefaultSize, 0) self.m_staticText1.Wrap(-1) bSizer1.Add(self.m_staticText1, 0, wx.ALL, 5) self.m_textCtrl1 = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0) bSizer1.Add(self.m_textCtrl1, 0, wx.ALL, 5) self.m_staticText2 = wx.StaticText(self, wx.ID_ANY, u"副窗口", wx.DefaultPosition, wx.DefaultSize, 0) self.m_staticText2.Wrap(-1) bSizer1.Add(self.m_staticText2, 0, wx.ALL, 5) self.m_textCtrl2 = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0) bSizer1.Add(self.m_textCtrl2, 0, wx.ALL, 5) self.m_button1 = wx.Button(self, wx.ID_ANY, u"复制", wx.DefaultPosition, wx.DefaultSize, 0) bSizer1.Add(self.m_button1, 0, wx.ALL, 5) self.m_button2 = wx.Button(self, wx.ID_ANY, u"清空", wx.DefaultPosition, wx.DefaultSize, 0) bSizer1.Add(self.m_button2, 0, wx.ALL, 5) self.SetSizer(bSizer1) self.Layout() self.Centre(wx.BOTH) # Connect Events self.m_button1.Bind(wx.EVT_BUTTON, self.copy_0) self.m_button2.Bind(wx.EVT_BUTTON, self.clear_0) def __del__(self): pass # Virtual event handlers, overide them in your derived class def copy_0(self, event): event.Skip() def clear_0(self, event): event.Skip()
然后是调用程序:
import wx import t1st_0 class t1st_2(t1st_0.test_0): def text_1(self): self.m_textCtrl1.SetValue(‘你好‘) def clear_0(self,event):#清空窗口中的内容 self.m_textCtrl1.Clear() self.m_textCtrl2.Clear() def copy_0(self,event): self.text_11 = self.m_textCtrl1.GetValue() self.m_textCtrl2.SetValue(self.text_11) if __name__ == ‘__main__‘: app = wx.App() # None表示的是此窗口没有上级父窗体。如果有,就直接在父窗体代码调用的时候填入‘self’就好了。 main_win = t1st_2(None) main_win.text_1()#设置窗口初值 main_win.Show() app.MainLoop()
时间: 2024-10-07 19:32:57