在显示大型机之前从对话框获取用户输入

时间:2016-04-06 21:11:26

标签: python python-2.7 wxpython

我有wxPython GUI程序,必须从用户那里获得输入才能运行。我想在主框架之前显示对话框,存储输入,关闭对话框然后运行主程序。现在我正在使用raw_input。这是我的代码:

import wx
import wx.lib.iewin as iewin
import subprocess

class MyBrowser(wx.Frame):
  def __init__(self, parent, id):
    wx.Frame.__init__(self, parent, id, 
                       style=wx.DEFAULT_FRAME_STYLE | wx.STAY_ON_TOP)
    self.transparency = 255

    sizer = wx.BoxSizer(wx.VERTICAL)
    self.browser =  iewin.IEHtmlWindow(self)
    sizer.Add(self.browser, 1, wx.EXPAND, 10)

    self.Bind(wx.EVT_CHAR_HOOK, self.onKey)

  def SetTransparent(self, value):
    self.transparency = value
    wx.Frame.SetTransparent(self, value) 

  def GetTransparent(self):
    return self.transparency   

  def decreaseTransparency(self, e):
    self.SetTransparent(self.GetTransparent() - 10) 
  def increaseTransparency(self, e):
    self.SetTransparent(self.GetTransparent() + 10)  


  def onKey(self, evt):
    if evt.GetKeyCode() == wx.WXK_DOWN:
      self.decreaseTransparency(self)
    elif evt.GetKeyCode() == wx.WXK_UP:
      self.increaseTransparency(self)
    else:
      evt.Skip()    

  def load(self,uri):
     self.browser.Navigate(uri)

#starts livestreamer process
response = raw_input("Livestreamers name:\n")
livestreamer = "livestreamer twitch.tv/"
host = subprocess.Popen(['livestreamer', 'twitch.tv/'+response, 'best'],    stdout=subprocess.PIPE)

if __name__ == '__main__':
app = wx.App()
dialog = MyBrowser(None, -1)
dialog.browser.Navigate("https://www.twitch.tv/" + response+ "/chat?popout=")
dialog.Show()
app.MainLoop()
host.communicate()[0]

这就是我的想法: dialog example

1 个答案:

答案 0 :(得分:1)

在你的浏览器中,init创建一个对话框,在其上运行ShowModal,然后使用dialog.GetValue获取输入

class MyBrowser(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id,
                   style=wx.DEFAULT_FRAME_STYLE | wx.STAY_ON_TOP)
        self.transparency = 255
        self.dialog = wx.TextEntryDialog(None, message="Enter stuff")
        self.dialog.ShowModal()
        print self.dialog.GetValue()
        self.Bind(wx.EVT_CHAR_HOOK, self.onKey)

显然,用您想要处理的值

替换打印

如果要在实例化MyBrowser对象后访问它,请将其指定为实例变量而不是打印。

class MyBrowser(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id,
                   style=wx.DEFAULT_FRAME_STYLE | wx.STAY_ON_TOP)
        self.transparency = 255
        self.dialog = wx.TextEntryDialog(None, message="Enter stuff")
        self.dialog.ShowModal()
        self.dlg_val = self.dialog.GetValue()
        self.Bind(wx.EVT_CHAR_HOOK, self.onKey)

然后进一步使用

if __name__ == "__main__":
    dialog = MyBrowser(None)
    print dialog.dlg_val