Question

I am trying to achieve the objectives of my project and connect my modules into window application with buttons. I don't know what am I missing so far but for sure something is wrong, because when my program is running then the main frame is crashed, no responding, Shell output works but no possibility to input anything... I guess i should show you all the code because i don't know exactly with part is wrong. I used the boa constructor to save some time to create Frames. The starting application looks like that:

App1:

import wx

import Frame1

modules ={'Frame1': [1, 'Main frame of Application', u'Frame1.py'],
 u'botcordxy': [0, u'x, y values', u'botcordxy.py']}

class BoaApp(wx.App):
    def OnInit(self):
        self.main = Frame1.create(None)
        self.main.Show()
        self.SetTopWindow(self.main)
        return True

def main():
    application = BoaApp(0)
    application.MainLoop()

if __name__ == '__main__':
    main()

I am not sure but i think what is above is correct, tell me if not.

Frame1.py:

import wx

def create(parent):
    return Frame1(parent)

[wxID_FRAME1, wxID_FRAME1BUTTON1, wxID_FRAME1PANEL1, 
] = [wx.NewId() for _init_ctrls in range(3)]

class Frame1(wx.Frame):
    def _init_ctrls(self, prnt):
        wx.Frame.__init__(self, id=wxID_FRAME1, name=u'Frame1', parent=prnt,
              pos=wx.Point(-1, 291), size=wx.Size(250, 480),
              style=wx.DEFAULT_FRAME_STYLE, title=u'ZulithBot')
        self.SetClientSize(wx.Size(242, 446))
        self.Bind(wx.EVT_BUTTON, self.Firefox, id=wxID_FRAME1BUTTON1)

        self.panel1 = wx.Panel(id=wxID_FRAME1PANEL1, name='panel1', parent=self,
              pos=wx.Point(0, 0), size=wx.Size(242, 446),
              style=wx.TAB_TRAVERSAL)

        self.button1 = wx.Button(id=wxID_FRAME1BUTTON1,
              label=u'Start Firefox', name='button1', parent=self.panel1,
              pos=wx.Point(80, 24), size=wx.Size(88, 23), style=0)

    def Firefox(self, event):
        import botcordxy
    def __init__(self, parent):
        self._init_ctrls(parent)

And now, the last:

botcordxy.py

import selenium
from selenium import webdriver
import time

##The name of website has been changed just in care.

driver = webdriver.Firefox()
driver.get('http://example.com//')

def repeat():
    while 1 == 1:
        botloc = driver.find_element_by_id('botloc').text
        botX,botY = map(int,botloc.split(','))
        print botX
        print botY
        print botloc

def checker():
    if driver.current_url == 'http://logged.example.com//':
        repeat()
    else:
        time.sleep(5)
        checker()

checker()

As for the last part, here began the stairs, a lot of problems, a lot of editing, plenty of time devoted to the activity...

When I run the program and log on in Webdriver browser, the then Shell shows the values ​​that I wanted to get:

32
59
32,59
31
59
31,59
31
58
31,58

over and over is printing botloc, botx and boty so the application is still ruining, but its frozen, no control till i use ctrl + C, Frame1 totally unavailable... Is there a lot of things that are missing? def respond loop can be operated in this a way? Can you please help me to fix that?

Was it helpful?

Solution

When writing wxPython apps, you have to ensure any event handler functions you've defined return fairly quickly, otherwise the app won't be able to process any other events, like repainting the frame, and your program will become unresponsive.

The problem is this event handler...

def Firefox(self, event):
    import botcordxy

...which ultimately causes this infinite loop to run...

while 1 == 1:
    botloc = driver.find_element_by_id('botloc').text
    botX,botY = map(int,botloc.split(','))
    print botX
    print botY
    print botloc

...so control will never return to the main event loop, and the main frame will appear to be frozen.

A quick n' dirty solution would be to temporarily yield control to the event handler inside the loop with wx.Yield(), like this...

import wx
while 1 == 1:
    botloc = driver.find_element_by_id('botloc').text
    botX,botY = map(int,botloc.split(','))
    print botX
    print botY
    print botloc
    wx.Yield()

...to give the application the opportunity to process other events, but you'd be better off looking a using a wx.Timer to call your driver.find_element_by_id(...) at regular intervals instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top