Question

I've done some tests, and, dunno why, GetValue() prints what I type if the parent is set to self, the frame, but not if it's set to self.panel.I need the TextCtrl to be in the panel. Where's the mistake, and how to make it work properly?

import wx
class APP(wx.Frame):
    def __init__(self,parent,id,title):
        wx.Frame.__init__(self,parent,id,title)
        self.panel=wx.Panel(self)
        self.INPUT = wx.TextCtrl(self,-1,value="")  ############ Here
        self.Bind(wx.EVT_TEXT_ENTER, self.ONPRESSENTER, self.INPUT)

    def ONPRESSENTER(self,event):
        print self.INPUT.GetValue()

if __name__ == "__main__":
    app = wx.App()
    frame = APP(None,-1,'TextCtrl in a panel - GetValue Test')
    frame.Show(True)
    app.MainLoop()

It will work this way,and we're out of the panel, but simply change wx.TextCtrl(self,-1,value="") into self.panel, and it won't print the string we type in.

Forgive me if it's something stupid, im a noob :D

Python 2.7

Était-ce utile?

La solution

The TextCtrl should have the wx.Panel as the parent. You should always have a panel as the first and only child widget in a frame as it adds tabbing support and makes the app look correct on all platforms. If you want to catch every text event, you want wx.EVT_TEXT, not wx.EVT_TEXT_ENTER. See the edited example below:

import wx

class APP(wx.Frame):
    def __init__(self,parent,id,title):
        wx.Frame.__init__(self,parent,id,title)
        self.panel=wx.Panel(self)
        self.INPUT = wx.TextCtrl(self.panel,-1,value="")  ############ Here
        self.Bind(wx.EVT_TEXT, self.ONPRESSENTER, self.INPUT)

    def ONPRESSENTER(self,event):
        print self.INPUT.GetValue()

if __name__ == "__main__":
    app = wx.App()
    frame = APP(None,-1,'TextCtrl in a panel - GetValue Test')
    frame.Show(True)
    app.MainLoop()
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top