如何阻止while循环中的Tkinter冻结?

时间:2019-02-15 20:38:25

标签: python python-3.x tkinter python-multithreading

我正在尝试为我的代码创建一个GUI。每次将新内容复制到剪贴板时,它都会在ebay上搜索这些已售商品。我希望我的代码每次复制某些内容时始终运行ebay函数。我遇到的问题是我想要一个关闭该功能的关闭按钮,因此我可以保持GUI打开并随意打开和关闭它。我尝试了线程,root.after和其他一些尝试来修复我的代码,但是每次按下on按钮时,GUI仍然冻结。我在做什么错/如何在始终从剪贴板中搜索新值的同时保持GUI正常运行?

import threading
import sys
import os
import webbrowser
sys.path.append(os.path.abspath("SO_site-packages"))
import pyperclip
import tkinter as tk

run = True

def ebay():
    current=""
    while run == True:
        new = pyperclip.paste()

        if new != current:
            current = new
            webbrowser.open('https://www.ebay.ca/sch/i.html?_from=R40&_nkw=' + str(new) + '&LH_Sold=1&LH_Complete=1&LH_ItemCondition=3000')


thread = threading.Thread()
thread.start()


def switchon():
    global run
    run = True
    ebay()

def switchoff():
    global run
    run = False

def main():
    root = tk.Tk()
    root.title("EbayPaste")
    root.geometry('400x300')
    onbutton = tk.Button(root, text="ON", command=switchon)
    onbutton.pack()
    offbutton = tk.Button(root, text="OFF", command=switchoff)
    offbutton.pack()

    root.mainloop()

thread2 =threading.Thread
thread2.start(main())

if __name__ == '__main__':
    main()

1 个答案:

答案 0 :(得分:0)

您对threading.Thread()的使用不太正确,因为它没有有效的目标。另外,我不确定您为什么要启动/停止该程序,因此在我的解决方案中,我已经取消了该功能(尽管如果您 REALLY 希望您可以找到一种方法来获得它) 。大大简化的代码可以完成我试图完成的工作(在eBay上搜索剪贴板中的所有内容):

import threading
import sys
import os
import webbrowser
sys.path.append(os.path.abspath("SO_site-packages"))
import pyperclip
import tkinter as tk


def ebay():
    current = ""
    new = pyperclip.paste()
    if new != current:
        current = new
        webbrowser.open('https://www.ebay.ca/sch/i.html?_from=R40&_nkw=' + str(new) + '&LH_Sold=1&LH_Complete=1&LH_ItemCondition=3000')


def open_ebay():
    thread = threading.Thread(target=ebay)
    thread.start()
    thread.join()


def main():
    root = tk.Tk()
    root.title("EbayPaste")
    root.geometry('400x300')
    tk.Button(root, text="load ebay from clipboard", command=open_ebay).pack()
    root.mainloop()


if __name__ == '__main__':
    main()

@EDIT

好的,我想这会满足您的需求。您将需要安装硒,我将为您推荐https://selenium-python.readthedocs.io/installation.html

import threading
import pyperclip
import tkinter as tk
import time
from selenium import webdriver


class myGUI:
    def __init__(self):
        self.thread_running = bool
        self.win = tk.Tk()
        self.win.title("EbayPaste")
        self.win.geometry('400x300')
        tk.Button(self.win, text="load ebay from clipboard", command=self.start_thread).pack()
        tk.Button(self.win, text="Stop thread", command=self.close_thread).pack()

    def close_thread(self):
        self.thread_running = False

    def start_thread(self):
        self.thread_running = True
        thread = threading.Thread(target=self.ebay)
        thread.start()

    def ebay(self):
        self.driver = webdriver.Chrome()
        old = ""
        loop = 0
        while self.thread_running:
            print('running loop {}'.format(loop))
            loop += 1
            time.sleep(1)
            new = pyperclip.paste()
            if new != old:
                old = new
                self.driver.get('https://www.ebay.ca/sch/i.html?_from=R40&_nkw=' + str(new) + '&LH_Sold=1&LH_Complete=1&LH_ItemCondition=3000')
        self.driver.quit()


if __name__ == '__main__':
    app = myGUI()
    app.win.mainloop()

这将产生一个线程,并且每1秒钟检查剪贴板中的更改。如果有更改,它将在产生它的浏览器中将其上拉。当用户关闭线程时,浏览器将自动关闭自身。通过这种方法,GUI不会冻结!如果这样做可以解决您的问题,那么您可以单击帖子旁的对勾以接受答案,那将是很好的选择。如果您真的很喜欢,也可以给它+ V投票:D