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

标签 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

最佳答案

在浏览器中创建一个对话框,在其上运行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

关于python - 在显示大型机之前从对话框获取用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36462432/

相关文章:

python - Python 3 中排序函数与比较函数的等效项

python - wxpython - 将 PIL.ImageFont 转换为 wx.Font 或 wx.Bitmap

python - 强制 pandas 在输出文件中存储类型信息

python-2.7 - 如何进行 GQL 查询(使用 gcloud-python)

python - 打开 cv 关闭相机

python - 有没有办法检测 python 程序何时结束?

python - 将 wxPython 与现有的 c++ Opengl 上下文一起使用

python - 缩进错误: unindent does not match any outer indentation level python

python - python netCDF4 中的纬度和经度表示

Python 缓冲 IO 以多管道结束早期流式传输 [BOUNTY]