wxPython的我怎么检索通过一个按钮

问题描述:

我试图从被使用wxPython中使用此代码按钮单击事件打开一个文本输入对话框中提取数据,打开一个对话框的信息。wxPython的我怎么检索通过一个按钮

import wx 
class apple(wx.Frame): 
    def __init__(self, parent, id): 
     wx.Frame.__init__(self, parent, id, 'PyLabs', size=(840,600)) 
     panel = wx.Panel(self) 
     box = wx.TextEntryDialog(None, 'hi', 'hi', 'hi') 


     status_bar = self.CreateStatusBar() 
     menu_bar = wx.MenuBar() 
     options_menu = wx.Menu() 
     options_menu.Append(wx.NewId(), "Settings", "OpenSettings...") 
     options_menu.Append(wx.NewId(), "Advanced", "Check Advanced...") 
     menu_bar.Append(options_menu, "Options") 
     self.SetMenuBar(menu_bar) 

     New_Experiment_Button = wx.Button(panel, pos=(10,10), label='New Experiment', size=(120, 40)) 
     answer = self.Bind(wx.EVT_BUTTON, self.openFrame, New_Experiment_Button) 
     print(answer) 


    def openFrame(self, event): 
     box = wx.TextEntryDialog(None, 'hi', 'hi', 'hi') 
     if box.ShowModal() == wx.ID_OK: 
      answer = str(box.getValue) 
      event.Skip() 
     return answer 


if __name__=='__main__': 
    app=wx.PySimpleApp() 
    frame = apple(parent=None, id=-1) 
    frame.Show() 
    app.MainLoop() 

我非常新的wxPython编码,我不明白我是如何抓住由当它从bind()函数中调用按钮事件拉着数据。

打印(回答)的输出是“无”

如果有人能帮助我解决这个它会不胜感激!

为什么不只是在功能上打印,为什么你需要把它恢复到?

def openFrame(self, event): 
    box = wx.TextEntryDialog(None, 'hi', 'hi', 'hi') 
    if box.ShowModal() == wx.ID_OK: 
     answer = box.GetValue() 
     event.Skip() 
    print answer 
+0

我需要使用的答案从对话框中被用作另一个函数的变量以后 –

的“答案”是box.GetValue(),但是你有你的实验几个问题,请参见下面,检查

编辑参考您的意见分歧,商店,你导致self变量,请参阅按钮上的Bind语句的区别。

import wx 
class apple(wx.Frame): 
    def __init__(self, parent, id): 
     wx.Frame.__init__(self, parent, id, 'PyLabs', size=(840,600)) 
     panel = wx.Panel(self) 
     status_bar = self.CreateStatusBar() 
     menu_bar = wx.MenuBar() 
     options_menu = wx.Menu() 
     options_menu.Append(wx.NewId(), "Settings", "OpenSettings...") 
     options_menu.Append(wx.NewId(), "Advanced", "Check Advanced...") 
     menu_bar.Append(options_menu, "Options") 
     self.SetMenuBar(menu_bar) 
     New_Experiment_Button = wx.Button(panel, pos=(10,10), label='New Experiment', size=(130, 30)) 
     New_Experiment_Button.Bind(wx.EVT_BUTTON, self.openFrame) 
     Result_Button = wx.Button(panel, pos=(10,60), label='Result of Experiment', size=(130, 30)) 
     Result_Button.Bind(wx.EVT_BUTTON, self.resultValue) 
     self.Text = wx.TextCtrl(panel, -1, "Nothing yet", pos=(10,100), size=(200,30)) 

    def openFrame(self, event): 
     box = wx.TextEntryDialog(None, 'What is your answer', 'Heading','Hi') 
     if box.ShowModal() == wx.ID_OK: 
      self.answer = box.GetValue() 
     box.Destroy() 

    def resultValue(self, event): 
     try: 
      self.Text.SetValue(self.answer) 
     except: 
      self.Text.SetValue("No answer yet supplied") 

if __name__=='__main__': 
    app=wx.App() 
    frame = apple(parent=None, id=-1) 
    frame.Show() 
    app.MainLoop() 
+0

我需要得到答案的值到另一个函数 –

+0

以后使用作为一个变量谢谢你您的输入,但我真正需要使用的答案从entrydialog作为一个变量在另一个函数以后 –

+0

@JuliusMartin见编辑答案 –