WxPython - 按钮>文本输入窗口>在新面板中输出
问题描述:
我想单击一个按钮,显示一个“文本输入框”,然后打印按钮所在的原始面板中键入的值。WxPython - 按钮>文本输入窗口>在新面板中输出
我设法让按钮+框工作,但我不知道如何显示面板中的值。
我很新的这一切:)
下面的代码:
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()
答
这里的问题是,你的面板对象超出范围一旦“INIT结束“方法已经达到,因此你不能在你的”opcao1“方法中使用它。如果在您的代码中将“panel”替换为代码中的“self.panel”,那么它将按照您的预期工作。 “self”使该变量成为一个类属性。
非常感谢。这解决了问题。 –