How to make Tkinter and Selenium work together

Viewed 249

Hello , my question is how can i make command a and b work at the same time , i tried threading but the thread executes the command immediatelly , thanks in advace for the help ! I am a beginner programmer so any help with this issue would help a lot , this project is intended to be a refresh bot but i can't figure it out !

import tkinter as tk
from time import sleep    
from selenium import webdriver



def a():
    driver1 = webdriver.Chrome(executable_path="/Users/user/Downloads/chromedriver")#your file 
    driver1.get(entry.get())
    while True:
        sleep(5)
        driver1.refresh()


def b():
    app.destroy()

def c():
    entry.delete(0, tk.END)



app = tk.Tk()

app.title("Youtube Bot")

app.geometry("425x350")

app.resizable(False, False)

app.configure(bg="#333436")


textOne = tk.Label(
 text = "Youtube Bot",
 font = ("Helvetica", 28),
 fg = "white",
 bg = "#333436",
 height = "1"
)
textOne.grid(pady = 10)

entry = tk.Entry(
 app,
 width = 25,
 font = ("Arial", 15),
 fg = "white",
 bg = "#6b6d75"
)
entry.focus_set()
entry.grid(padx=10, pady=18 )
entry.get()
entry.insert(0, "Paste Video Link")

buttonRefresh = tk.Button(
 text = "Delete",
 font = ("Arial", 15),
 width = 8,
 command = c

)
buttonRefresh.grid(padx = 6)

buttonOne = tk.Button(
 text = "Start process",
 font =("Helvetica", 20),
 height = "3",
 width = "20",
 command = a
)
 buttonOne.grid()

buttonTwo = tk.Button(
 text="Stop process",
 font=("Helvetica", 20),
 height="3",
 width="20",
 command = b
)
 buttonTwo.grid(pady=20)

 textOne.grid(row = 0, column = 1)
 entry.grid(row = 1, column = 1)
 buttonRefresh.grid(row = 1, column = 0)
 buttonOne.grid(row = 2, column = 1)
 buttonTwo.grid(row = 3, column = 1)

 app.mainloop()
1 Answers

This is where the thread runs, to call this thread, you would do a().start()


class a(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
    def run(self):
        global driver1
        driver1 = webdriver.Chrome(executable_path="/Users/user/Downloads/chromedriver")
        driver1.get(entry.get())
        while True:
            sleep(5)
            driver1.refresh()

Note I have defined driver1 so I can access it and close it when you call the function b

I changed this line:

buttonOne = tk.Button(text = "Start process",font =("Helvetica", 20),height = "3",width = "20",command = a)

To:

buttonOne = tk.Button(text = "Start process",font =("Helvetica", 20),height = "3",width = "20",command = lambda: a().start())

Becuase:

  • Since its a().start()) not just a, you need to include lambda, so the class a is only called when buttonOne is pressed.

I also included driver1.close() in the function b to also close the browser when you quit the app.

Full code:

from time import sleep
from selenium import webdriver
import threading

class a(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
    def run(self):
        global driver1
        driver1 = webdriver.Chrome(executable_path="/Users/user/Downloads/chromedriver")
        driver1.get(entry.get())
        while True:
            sleep(5)
            driver1.refresh()

def b():
    app.destroy()
    driver1.close()

def c():
    entry.delete(0, tk.END)



app = tk.Tk()

app.title("Youtube Bot")

app.geometry("425x350")

app.resizable(False, False)

app.configure(bg="#333436")


textOne = tk.Label(text = "Youtube Bot",font = ("Helvetica", 28),fg = "white",bg = "#333436",height = "1")
textOne.grid(pady = 10)

entry = tk.Entry(app,width = 25,font = ("Arial", 15),fg = "white",bg = "#6b6d75")
entry.focus_set()
entry.grid(padx=10, pady=18 )
entry.get()
entry.insert(0, "Paste Video Link")

buttonRefresh = tk.Button(text = "Delete",font = ("Arial", 15),width = 8,command = c)
buttonRefresh.grid(padx = 6)

buttonOne = tk.Button(text = "Start process",font =("Helvetica", 20),height = "3",width = "20",command = lambda: a().start())

buttonOne.grid()

buttonTwo = tk.Button(text="Stop process",font=("Helvetica", 20),height="3",width="20",command = b)
buttonTwo.grid(pady=20)

textOne.grid(row = 0, column = 1)
entry.grid(row = 1, column = 1)
buttonRefresh.grid(row = 1, column = 0)
buttonOne.grid(row = 2, column = 1)
buttonTwo.grid(row = 3, column = 1)

app.mainloop()

The line driver1 = webdriver.Chrome(executable_path="/Users/user/Downloads/chromedriver") was originally defined outside class a

This meant it looked something like this

driver1 = webdriver.Chrome(executable_path="/Users/user/Downloads/chromedriver")

class a(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
    def run(self):
        driver1.get(entry.get())
        while True:
            sleep(5)
            driver1.refresh()

This would mean that every time you start the program driver1would be automatically defined, resulting in the browser opening.

So I just moved driver1 in class a so that when buttonOne is pressed, that is when driver1 would be defined, and then that's when the browser would open.

I also made driver1 a global variable so you can access it when calling the function:

def b():
    app.destroy()
    driver1.close()
Related