如何在wxpython中将TextCtrl的函数控制为动态函数



我是wxpython用户。我有一个关于如何控制TextCtrl函数的问题。我想要构建一个程序的是,如果你点击某个按钮,TextCtrl的值就会改变。下面是我的源代码。

#!/usr/bin/python  
import wx
class Tom(wx.Dialog):
    def __init__(self, parent, id, title):
        wx.Dialog.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(400, 420))
        panel = wx.Panel(self, -1)
        vbox = wx.BoxSizer(wx.HORIZONTAL)
        self.write = wx.TextCtrl(panel, -1, style=wx.TE_MULTILINE, value=self.message)
        button_send = wx.Button(panel, 1, label='b'+'1', 'Send')
        vbox.Add(self.write, 1, wx.EXPAND | wx.TOP | wx.RIGHT | wx.LEFT | wx.BOTTOM, 15)
        vbox.Add(button_send, 0, wx.ALIGN_CENTER | wx.TOP | wx.BOTTOM, 20)
        self.Bind(wx.EVT_BUTTON, self.OnSend, id=1)
        panel.SetSizer(vbox)
        self.Centre()
    def OnSend(self, event):
        b=event.GetEventObject().GetLabel()
        if b.endswith('1'):
            self.message = "sss"
            self.setWrite(self.message)

class MyApp(wx.App):
    def OnInit(self):
        frame = Tom(None, -1, 'Tom')
        frame.ShowModal()
        frame.Destroy()
        return True
app = MyApp(0)
app.MainLoop()

如果谁知道解决方案,请评论我。谢谢。

使用self.write.SetValue()
略有改动,使用python-gtk3.0 运行

#!/usr/bin/python  
import wx
class Tom(wx.Dialog):
    def __init__(self, parent, id, title):
        wx.Dialog.__init__(self, parent, id, title, wx.DefaultPosition, wx.Size(400, 420))
        panel = wx.Panel(self, -1)
        vbox = wx.BoxSizer(wx.HORIZONTAL)
        self.message=""
        self.write = wx.TextCtrl(panel, -1, style=wx.TE_MULTILINE, value=self.message)
        button_send = wx.Button(panel, 1, label='b1')
        vbox.Add(self.write, 1, wx.EXPAND | wx.TOP | wx.RIGHT | wx.LEFT | wx.BOTTOM, 15)
        vbox.Add(button_send, 0, wx.ALIGN_CENTER | wx.TOP | wx.BOTTOM, 20)
        self.Bind(wx.EVT_BUTTON, self.OnSend, id=1)
        panel.SetSizer(vbox)
        self.Centre()
    def OnSend(self, event):
        b=event.GetEventObject().GetLabel()
        if b.endswith('1'):
            self.message = self.message+"sss"
            self.write.SetValue(self.message)

class MyApp(wx.App):
    def OnInit(self):
        frame = Tom(None, -1, 'Tom')
        frame.ShowModal()
        frame.Destroy()
        return True
app = MyApp()
app.MainLoop()

相关内容

  • 没有找到相关文章

最新更新