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

时间:2016-05-09 08:50:34

标签: python dynamic 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()

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

1 个答案:

答案 0 :(得分:1)

使用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()