我正在尝试为我的代码创建一个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()
你使用threading.Thread()
不太正确,因为它没有有效的目标。此外,我不确定你为什么要启动/停止程序,所以在我的解决方案中我已经取出了这个功能(虽然如果你真的想要你可以找到一种方法来获得它)。完全简化的代码,我认为你想要完成的任务(在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()
@编辑
好的,我想这会得到你想要的东西。你需要安装selenium,我会把你推荐给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不会冻结!如果这有助于解决您的问题,那么如果您可以通过帖子点击该复选标记来接受答案就会很棒。如果你真的喜欢它,你也可以给它+投票:D