user6160169
user6160169

Reputation:

Get user input from a dialog before showing the mainframe

I have wxPython GUI program that has to get an input from the user to run. I want to show the dialog before the main frame, store the input, close the dialog and then run the main program. Right now I am using raw_input instead. This is my code:

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]

This is what I have in mind: dialog example

Upvotes: 1

Views: 147

Answers (1)

BretD
BretD

Reputation: 366

In your browser init create a dialog box, run ShowModal on it, and then get the input with 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)

Obviously replace printing it with whatever you want to do with the value

If you want to access it after instantiation of the MyBrowser object assign it as an instance variable rather than printing.

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)

Then use it further down

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

Upvotes: 1

Related Questions