I want to click a button, have a “text input box” show up and then print the value typed in the original panel where the button is.
I managed to get the button + box working but I can’t figure out how to display the value in the panel.
I’m pretty new to all this 🙂
Here’s the code:
import wx
class st3000(wx.Frame):
def __init__(self,parent,id):
wx.Frame.__init__(self,parent,id,'title', size=(353,270))
panel=wx.Panel(self)
button1=wx.Button(panel,label='something',pos=(10,10),size=(-1,60))
button2=wx.Button(panel,label='anything', pos=(150,10),size=(-1,60))
button3=wx.Button(panel,label='nothing', pos=(250,10),size=(-1,60))
self.Bind(wx.EVT_BUTTON, self.opcao1, button1)
self.Bind(wx.EVT_BUTTON, self.opcao2, button2)
self.Bind(wx.EVT_BUTTON, self.opcao3, button3)
self.Bind(wx.EVT_CLOSE, self.closewindow)
def opcao1(self,event):
box1=wx.TextEntryDialog(None,'Type...','','...here')
if box1.ShowModal()==wx.ID_OK:
answer1=box1.GetValue()
output=wx.StaticText(panel,-1,answer1,(10,80),(260,-1),wx.ALIGN_CENTER)
output.SetForegroundColour('red')
output.SetBackgroundColour('blue')
def opcao2(self,event):
self.Close(True)
def opcao3(self,event):
self.Close(True)
def closewindow(self,event):
self.Destroy()
if __name__=='__main__':
app=wx.PySimpleApp()
frame=st3000(parent=None,id=-1)
frame.Show()
app.MainLoop()
The problem here is that your panel object goes out of scope once the end of the “init” method is reached, thus you cannot use it in your “opcao1” method. If you replace “panel” with “self.panel” in your code where ever it’s referenced, then it will work as you intended. The “self” makes the variable a class property.