0

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?

4

1 回答 1

0

在编写 wxPython 应用程序时,您必须确保您定义的任何事件处理函数能够快速返回,否则应用程序将无法处理任何其他事件,例如重新绘制框架,并且您的程序将变得无响应。

问题是这个事件处理程序......

def Firefox(self, event):
    import botcordxy

...最终导致这个无限循环运行...

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(),就像这样......

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()

...让应用程序有机会处理其他事件,但你最好寻找一个使用 a定期wx.Timer调用你的方法。driver.find_element_by_id(...)

于 2013-05-18T12:55:11.090 回答