Skip to content Skip to sidebar Skip to footer

How To Stop Tkinter From Freezing From While Loop?

I am trying to create a GUI for my code. Every time something new is copied to the clipboard it will search up those sold listings on ebay. I want my code to always run the ebay fu

Solution 1:

Your use of threading.Thread() is not quite correct, as it has no valid target. Also, I'm not sure why you want to start/stop the program so in my solution I have taken that functionality out (although if you REALLY wanted to you could figure out a way to get it). The drastically simplified code that does what I think you are trying to accomplish (search on ebay everything you have in the clipboard):

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

OK I think this will get what you were looking for. You will need to install selenium for which I will refer you to 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()

This will spawn a thread and every 1 second check your clipboard for changes. If there is a change, it will pull it up in the browser that it spawns in. When the user turns off the thread, the browser will close itself automatically. Through this method the GUI does not freeze! If this helped solve your problem it'd be great if you could click that check mark by the post to accept the answer. If you REALLY liked it you could give it a +vote as well :D


Post a Comment for "How To Stop Tkinter From Freezing From While Loop?"