Problem using wxPython

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • somialis
    New Member
    • Mar 2007
    • 32

    Problem using wxPython

    Hi,
    i have a GUI and my GUI Toolkit is wxPython. Now when the GUI opens...whateve r tabs and buttons i click...an action is performed. Each mouse click is an event and it is processed in the script accordingly.

    Now i want to operate that GUI without using the mouse..i'e thru my script....

    for example...in the original python script...there is a function "OnBrowseForRep ort Directory". When i click on the tab for browse in the GUI....a dialog opens from where i can choose my report directory and click on OK. The directory gets chosen...but all these actions are performed on the click of the mouse...i.e manually. I want to automate the entire thing...i.e I want to open the dialog thru my own python script and choose the directory without clicking on the "OK" tab...i.e thru my script.....i want to use the underlyting fumction "OnBrowseForRep ort Directory"...

    Plz help...it's urgent!!
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    Originally posted by somialis
    Hi,
    i have a GUI and my GUI Toolkit is wxPython. Now when the GUI opens...whateve r tabs and buttons i click...an action is performed. Each mouse click is an event and it is processed in the script accordingly.

    Now i want to operate that GUI without using the mouse..i'e thru my script....

    for example...in the original python script...there is a function "OnBrowseForRep ort Directory". When i click on the tab for browse in the GUI....a dialog opens from where i can choose my report directory and click on OK. The directory gets chosen...but all these actions are performed on the click of the mouse...i.e manually. I want to automate the entire thing...i.e I want to open the dialog thru my own python script and choose the directory without clicking on the "OK" tab...i.e thru my script.....i want to use the underlyting fumction "OnBrowseForRep ort Directory"...

    Plz help...it's urgent!!
    "OnBrowseForRep ort Directory" can't really be the name because of the space in there (I'll use "OnBrowseForRep ort" in my explanation:
    The easiest way to do this is to add a button (or menu item, or whaterver) to start the action you want. Then you can call self.OnBrowseFo rReport(None) as long as the event handler never calls any event.WhatEver( )s. And so on, until all actions are complete.

    Comment

    • somialis
      New Member
      • Mar 2007
      • 32

      #3
      Originally posted by bartonc
      "OnBrowseForRep ort Directory" can't really be the name because of the space in there (I'll use "OnBrowseForRep ort" in my explanation:
      The easiest way to do this is to add a button (or menu item, or whaterver) to start the action you want. Then you can call self.OnBrowseFo rReport(None) as long as the event handler never calls any event.WhatEver( )s. And so on, until all actions are complete.
      Cudn't get u...let me explain it a bit....
      thr's one function which looks like this
      Code:
          def OnBrowseForDeviceWrapperFile(self, event):
              '''Allows the user to select a 'device wrapper' file via a dialog.'''
              
              dlg = wx.FileDialog(
                  self, message="Choose a Device Wrapper file",
                  wildcard=deviceWrapperFileFilter, style=wx.OPEN|wx.CHANGE_DIR)
              
              if dlg.ShowModal() == wx.ID_OK:
                  
                  # get the new filename from the dialog, then update the test setup
                  # and GUI to reflect the change...
                  filename = dlg.GetPath()
                  self.testSetup.DeviceWrapperFilename = filename
                  self.textCtrls['DeviceWrapperFilename'].SetValue(filename)
                  self.refreshSetupModifiedFlag()
                  
              dlg.Destroy()
      this function takes an event as input. I don't want to select the directory using mouse click.
      How do i go about it??

      Comment

      • bartonc
        Recognized Expert Expert
        • Sep 2006
        • 6478

        #4
        Originally posted by somialis
        Cudn't get u...let me explain it a bit....
        thr's one function which looks like this
        Code:
            def OnBrowseForDeviceWrapperFile(self, event):
                '''Allows the user to select a 'device wrapper' file via a dialog.'''
                
                dlg = wx.FileDialog(
                    self, message="Choose a Device Wrapper file",
                    wildcard=deviceWrapperFileFilter, style=wx.OPEN|wx.CHANGE_DIR)
                
                if dlg.ShowModal() == wx.ID_OK:
                    
                    # get the new filename from the dialog, then update the test setup
                    # and GUI to reflect the change...
                    filename = dlg.GetPath()
                    self.testSetup.DeviceWrapperFilename = filename
                    self.textCtrls['DeviceWrapperFilename'].SetValue(filename)
                    self.refreshSetupModifiedFlag()
                    
                dlg.Destroy()
        this function takes an event as input. I don't want to select the directory using mouse click.
        How do i go about it??
        You may want to consider refactoring.
        Let's see if I can explain:
        Presumably, OnBrowseForDevi ceWrapperFile() is caused by a button or menu, etc., which gives an interactive (file dialog) means of getting file name.

        If you have the file name already you would refactor thusly:
        Code:
            def OnBrowseForDeviceWrapperFile(self, event):
                '''Allows the user to select a 'device wrapper' file via a dialog.'''
                 filename = ""  # Use  filename as a flag
                dlg = wx.FileDialog(
                    self, message="Choose a Device Wrapper file",
                    wildcard=deviceWrapperFileFilter, style=wx.OPEN|wx.CHANGE_DIR)
                
                if dlg.ShowModal() == wx.ID_OK:
                    
                    # get the new filename from the dialog, then update the test setup
                    # and GUI to reflect the change...
                    filename = dlg.GetPath()
                dlg.Destroy()  # best to do this sooner than later
        
                if filename:
                    self.UpdateInterface(filename)
        
            def UpdateInterface(self, filename):
                self.testSetup.DeviceWrapperFilename = filename
                self.textCtrls['DeviceWrapperFilename'].SetValue(filename)
                self.refreshSetupModifiedFlag()
        By refactoring, you make the update code available to other methods.

        If you don't already have the file name and you are trying to (say) start your program with command-line arguments, those will be availble in the first module that is used to launch your program as in
        Code:
        # file 'MyModule.py'
        import sys
        print sys.argv
        The first argument is always the name of the first module as in
        Code:
        python 'MyModule.py
        ['MyModule.py']

        Comment

        • somialis
          New Member
          • Mar 2007
          • 32

          #5
          Originally posted by bartonc
          You may want to consider refactoring.
          Let's see if I can explain:
          Presumably, OnBrowseForDevi ceWrapperFile() is caused by a button or menu, etc., which gives an interactive (file dialog) means of getting file name.

          If you have the file name already you would refactor thusly:
          Code:
              def OnBrowseForDeviceWrapperFile(self, event):
                  '''Allows the user to select a 'device wrapper' file via a dialog.'''
                   filename = ""  # Use  filename as a flag
                  dlg = wx.FileDialog(
                      self, message="Choose a Device Wrapper file",
                      wildcard=deviceWrapperFileFilter, style=wx.OPEN|wx.CHANGE_DIR)
                  
                  if dlg.ShowModal() == wx.ID_OK:
                      
                      # get the new filename from the dialog, then update the test setup
                      # and GUI to reflect the change...
                      filename = dlg.GetPath()
                  dlg.Destroy()  # best to do this sooner than later
          
                  if filename:
                      self.UpdateInterface(filename)
          
              def UpdateInterface(self, filename):
                  self.testSetup.DeviceWrapperFilename = filename
                  self.textCtrls['DeviceWrapperFilename'].SetValue(filename)
                  self.refreshSetupModifiedFlag()
          By refactoring, you make the update code available to other methods.

          If you don't already have the file name and you are trying to (say) start your program with command-line arguments, those will be availble in the first module that is used to launch your program as in
          Code:
          # file 'MyModule.py'
          import sys
          print sys.argv
          The first argument is always the name of the first module as in
          Code:
          python 'MyModule.py
          ['MyModule.py']

          Hi,
          I did what u had asked, when i call the function "OnBrowseForDev iceWrapperFile" , it opens a dialog but i have to manually select the file and click on open. It is not doing it on it's own.

          Comment

          • bartonc
            Recognized Expert Expert
            • Sep 2006
            • 6478

            #6
            Originally posted by somialis
            Hi,
            I did what u had asked, when i call the function "OnBrowseForDev iceWrapperFile" , it opens a dialog but i have to manually select the file and click on open. It is not doing it on it's own.
            I suggested that you use
            Code:
                def UpdateInterface(self, filename):
                    self.testSetup.DeviceWrapperFilename = filename
                    self.textCtrls['DeviceWrapperFilename'].SetValue(filename)
                    self.refreshSetupModifiedFlag()
            and call it
            Code:
            self.UpdateInterface(aKnownFileName)

            Comment

            • somialis
              New Member
              • Mar 2007
              • 32

              #7
              Originally posted by bartonc
              I suggested that you use
              Code:
                  def UpdateInterface(self, filename):
                      self.testSetup.DeviceWrapperFilename = filename
                      self.textCtrls['DeviceWrapperFilename'].SetValue(filename)
                      self.refreshSetupModifiedFlag()
              and call it
              Code:
              self.UpdateInterface(aKnownFileName)
              i called UpdateInterface with a known filename sa input...this time the dialog did not open ...however the GUI remained empty i.e the column where the file name(after it is selected thru the GUI) is reflected, remained vacent.

              Comment

              • bartonc
                Recognized Expert Expert
                • Sep 2006
                • 6478

                #8
                Originally posted by somialis
                i called UpdateInterface with a known filename sa input...this time the dialog did not open ...however the GUI remained empty i.e the column where the file name(after it is selected thru the GUI) is reflected, remained vacent.
                Thats odd. If it works one way (file dialog get file name) it will work the other.
                Try
                Code:
                    def UpdateInterface(self, filename):
                        print filename
                        self.testSetup.DeviceWrapperFilename = filename
                        self.textCtrls['DeviceWrapperFilename'].SetValue(filename)
                        self.refreshSetupModifiedFlag()
                as a quick debugging test. If filename gets printed, it should also be Set in the TextCtrl.

                Are you working in an IDE that will show you if an error occured?

                Comment

                • somialis
                  New Member
                  • Mar 2007
                  • 32

                  #9
                  Originally posted by bartonc
                  Thats odd. If it works one way (file dialog get file name) it will work the other.
                  Try
                  Code:
                      def UpdateInterface(self, filename):
                          print filename
                          self.testSetup.DeviceWrapperFilename = filename
                          self.textCtrls['DeviceWrapperFilename'].SetValue(filename)
                          self.refreshSetupModifiedFlag()
                  as a quick debugging test. If filename gets printed, it should also be Set in the TextCtrl.

                  Are you working in an IDE that will show you if an error occured?
                  Hi,
                  My script looks something like this
                  Code:
                  import traceback
                  import thread
                  import pst
                  import petShopTest
                  
                  class newclass:
                  
                      def __init__(self):
                          newApp = petShopTest.MyApp(0)
                          newFrame = petShopTest.MyFrame(None, -1, "PetShopTest")
                          self.testsetup = pst.TestSetup()
                          self.Log = logging.getLogger('GUI')
                          path = "c:\pst\pst_modified\Flash_Test.py"
                          path1 = "c:\pst\pst_modified\DeviceCommsLayer_984_v6_00.py"
                          path2 = "c:\python23"
                  
                  
                          newFrame.UpdateInterfaceForDeviceWrapper(path1)
                  
                          newApp.MainLoop()
                  and the UpdateInterface ForDeviceWrappe r function looks like this:
                  Code:
                      def UpdateInterfaceForDeviceWrapper(self,filename):
                          print filename
                          print "in update"    
                          self.testSetup.DeviceWrapperFilename = filename
                          self.textCtrls['DeviceWrapperFilename'].SetValue(filename)
                          self.refreshSetupModifiedFlag()
                  i am executing this on python command promt window like this:

                  Code:
                  import myfilename
                  myfilename.newclass()
                  and it shows the following:

                  Code:
                  c:\pst\pst_modified\DeviceCommsLayer_984_v6_00.py
                  in update
                  and nothing appears on the GUI...the GUI remains vacant!!:(
                  It's not showing any errors.

                  Comment

                  • bartonc
                    Recognized Expert Expert
                    • Sep 2006
                    • 6478

                    #10
                    Originally posted by somialis
                    Hi,
                    My script looks something like this
                    Code:
                    import traceback
                    import thread
                    import pst
                    import petShopTest
                    
                    class newclass:
                    
                        def __init__(self):
                            newApp = petShopTest.MyApp(0)
                            newFrame = petShopTest.MyFrame(None, -1, "PetShopTest")
                            self.testsetup = pst.TestSetup()
                            self.Log = logging.getLogger('GUI')
                            path = "c:\pst\pst_modified\Flash_Test.py"
                            path1 = "c:\pst\pst_modified\DeviceCommsLayer_984_v6_00.py"
                            path2 = "c:\python23"
                    
                    
                            newFrame.UpdateInterfaceForDeviceWrapper(path1)
                    
                            newApp.MainLoop()
                    and the UpdateInterface ForDeviceWrappe r function looks like this:
                    Code:
                        def UpdateInterfaceForDeviceWrapper(self,filename):
                            print filename
                            print "in update"    
                            self.testSetup.DeviceWrapperFilename = filename
                            self.textCtrls['DeviceWrapperFilename'].SetValue(filename)
                            self.refreshSetupModifiedFlag()
                    i am executing this on python command promt window like this:

                    Code:
                    import myfilename
                    myfilename.newclass()
                    and it shows the following:

                    Code:
                    c:\pst\pst_modified\DeviceCommsLayer_984_v6_00.py
                    in update
                    and nothing appears on the GUI...the GUI remains vacant!!:(
                    It's not showing any errors.
                    That helps a lot!
                    Mainloop() has to be running before widgets will act.
                    Code:
                    class newclass:
                    
                        def __init__(self):
                            newApp = petShopTest.MyApp(0)
                            self.newFrame = newFrame = petShopTest.MyFrame(None, -1, "PetShopTest")
                            self.testsetup = pst.TestSetup()
                            self.Log = logging.getLogger('GUI')
                            path = "c:\pst\pst_modified\Flash_Test.py"
                            path1 = "c:\pst\pst_modified\DeviceCommsLayer_984_v6_00.py"
                            path2 = "c:\python23"
                    
                    
                            wx.FutureCall(100, self.SendFileName)
                    
                            newApp.MainLoop()
                    
                        def SendFileName(self):
                            self.newFrame.UpdateInterfaceForDeviceWrapper(path1)
                    But if you are calling from the command-line, why not
                    Code:
                    import sys
                    
                    
                            wx.FutureCall(100, self.GetFileName)
                    
                            newApp.MainLoop()
                    
                        def GetFileName(self):
                            filename = sys.argv[1]
                            self.newFrame.UpdateInterfaceForDeviceWrapper(filename)
                    python TestWhatever c:\pst\pst_modi fied\DeviceComm sLayer_984_v6_0 0.py

                    Comment

                    • somialis
                      New Member
                      • Mar 2007
                      • 32

                      #11
                      Originally posted by bartonc
                      That helps a lot!
                      Mainloop() has to be running before widgets will act.
                      Code:
                      class newclass:
                      
                          def __init__(self):
                              newApp = petShopTest.MyApp(0)
                              self.newFrame = newFrame = petShopTest.MyFrame(None, -1, "PetShopTest")
                              self.testsetup = pst.TestSetup()
                              self.Log = logging.getLogger('GUI')
                              path = "c:\pst\pst_modified\Flash_Test.py"
                              path1 = "c:\pst\pst_modified\DeviceCommsLayer_984_v6_00.py"
                              path2 = "c:\python23"
                      
                      
                              wx.FutureCall(100, self.SendFileName)
                      
                              newApp.MainLoop()
                      
                          def SendFileName(self):
                              self.newFrame.UpdateInterfaceForDeviceWrapper(path1)
                      But if you are calling from the command-line, why not
                      Code:
                      import sys
                      
                      
                              wx.FutureCall(100, self.GetFileName)
                      
                              newApp.MainLoop()
                      
                          def GetFileName(self):
                              filename = sys.argv[1]
                              self.newFrame.UpdateInterfaceForDeviceWrapper(filename)
                      python TestWhatever c:\pst\pst_modi fied\DeviceComm sLayer_984_v6_0 0.py
                      Did according to what u suggested. No change in the output. It is the same as before: :(
                      Code:
                      c:\pst\pst_modified\DeviceCommsLayer_984_v6_00.py
                      in update
                      and nothing was reflected in the GUI!! :(

                      Comment

                      • bartonc
                        Recognized Expert Expert
                        • Sep 2006
                        • 6478

                        #12
                        Originally posted by somialis
                        Did according to what u suggested. No change in the output. It is the same as before: :(
                        Code:
                        c:\pst\pst_modified\DeviceCommsLayer_984_v6_00.py
                        in update
                        and nothing was reflected in the GUI!! :(
                        I'll try to get some time to work up some running code later on today or tonight.
                        This one has me baffled, because that should have worked.

                        Comment

                        • somialis
                          New Member
                          • Mar 2007
                          • 32

                          #13
                          Originally posted by bartonc
                          I'll try to get some time to work up some running code later on today or tonight.
                          This one has me baffled, because that should have worked.
                          Hi,
                          Did u see it?? it's still not working!!

                          Comment

                          • somialis
                            New Member
                            • Mar 2007
                            • 32

                            #14
                            Originally posted by bartonc
                            I'll try to get some time to work up some running code later on today or tonight.
                            This one has me baffled, because that should have worked.
                            Hi,
                            Please help me out with the problem. Did u find some way out of it??

                            Comment

                            • bartonc
                              Recognized Expert Expert
                              • Sep 2006
                              • 6478

                              #15
                              Originally posted by somialis
                              Hi,
                              Please help me out with the problem. Did u find some way out of it??
                              I did a test:
                              Code:
                              #Boa:Frame:Frame2
                              
                              import wx
                              
                              def create(parent):
                                  return Frame1(parent)
                              
                              [wxID_FRAME1, wxID_FRAME1PANEL1, wxID_FRAME1TEXTCTRL1,
                              ] = [wx.NewId() for _init_ctrls in range(3)]
                              
                              class Frame1(wx.Frame):
                                  def _init_sizers(self):
                                      # generated method, don't edit
                                      self.boxSizer1 = wx.BoxSizer(orient=wx.VERTICAL)
                              
                                      self._init_coll_boxSizer1_Items(self.boxSizer1)
                              
                                      self.SetSizer(self.boxSizer1)
                              
                              
                                  def _init_coll_boxSizer1_Items(self, parent):
                                      # generated method, don't edit
                              
                                      parent.AddWindow(self.panel1, 1, border=0, flag=wx.EXPAND)
                              
                                  def _init_ctrls(self, prnt):
                                      # generated method, don't edit
                                      wx.Frame.__init__(self, id=wxID_FRAME1, name='', parent=prnt, pos=wx.Point(22, 22),
                                              size=wx.Size(400, 250), style=wx.DEFAULT_FRAME_STYLE, title='Frame1')
                                      self.SetClientSize(wx.Size(392, 223))
                              
                                      self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self, pos=wx.Point(0, 0),
                                              size=wx.Size(392, 223), style=wx.TAB_TRAVERSAL)
                              
                                      self.textCtrl1 = wx.TextCtrl(id=wxID_FRAME1TEXTCTRL1, name='textCtrl1', parent=self.panel1,
                                              pos=wx.Point(16, 72), size=wx.Size(360, 21), style=0, value='')
                              
                                      self._init_sizers()
                              
                                  def __init__(self, parent):
                                      self._init_ctrls(parent)
                              Code:
                              #!/usr/bin/env python
                              #Boa:App:BoaApp
                              
                              import wx
                              
                              import Frame2
                              
                              modules ={u'Frame2': [1, 'Main frame of Application', u'Frame2.py']}
                              
                              class BoaApp(wx.App):
                                  def OnInit(self):
                                      self.main = Frame2.create(None)
                                      ## this is NOT good OOP encapulation, but it works ##
                                      self.main.textCtrl1.SetValue('this is a test')
                                      #                                                   #
                                      self.main.Show()
                                      self.SetTopWindow(self.main)
                                      return True
                              
                              def main():
                                  application = BoaApp(0)
                                  application.MainLoop()
                              
                              if __name__ == '__main__':
                                  main()
                              I can set the TextCtrl Value while the app is being initialized.

                              Comment

                              Working...