I got my program running but whenever I run the script from the button (minknapp2) TKinter stops responding until the script has finished. I did some research and found out it's because I need to use threading, and I would be able to interact with TKinter while the script is running.
I have separated my program into 2 files which I am thinking is the issue. one file runs TKinter and the second file runs my Selenium scripts. filename1 is NyceGui.py filename2 is webreader.py
Tkinter file looks like this.
from tkinter import *
import webreader as wr
root = Tk()
#Skapa labels
minLabel1 = Label(root, text = "Nyce automation Gui")
minLabel2 = Label(root, text = "Automatisera processer")
minLabel3 = Label(root, text = "Flytta artikel")
minLabel4 = Label(root, text = "Artikelnummer: ")
minLabel5 = Label(root, text = "Från: ")
minLabel6 = Label(root, text = "Till: ")
#Skapa knappar
minKnapp1 = Button(root, text = "Öppna Nyce", command=wr.startaNyce)
minKnapp2 = Button(root, text = "Bekräfta aktiv order", command=wr.bekräfta
minKnapp3 = Button(root, text = "Bekräfta småplock aktiv order", command=wr.bekräftaSmå)
minKnapp4 = Button(root, text = "AVBRYT SCRIPT", command=wr.stopp, bg ="red")
#Skapa textfält
mittText1 = Entry(root)
mittText2 = Entry(root)
mittText3 = Entry(root)
#Visa på skärm
minLabel1.grid(row=0, column=0)
minLabel2.grid(row=1, column=0)
minKnapp1.grid(row=2, column=0)
minKnapp2.grid(row=3, column=0)
minKnapp3.grid(row=4, column=0)
minKnapp4.grid(row=1, column=3)
minLabel3.grid(row=6, column=1)
minLabel4.grid(row=7, column=0)
mittText1.grid(row=7, column=1)
minLabel5.grid(row=8, column=0)
mittText2.grid(row=8, column=1)
minLabel6.grid(row=8, column=2)
mittText3.grid(row=8, column=3)
root.mainloop()
And the Selenium file looks like this.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import sys
def startaNyce():
global driver
driver = webdriver.Chrome(executable_path=r'./chromedriver.exe')
driver.get("http://awebpage.com")
def bekräfta():
orange = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//input[contains(@class, 'nl-forms-wp-orange')]")))
try:
while orange:
text = WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[contains(@class, 'form-control nl-forms-wp-orange id-requisiteinfo-input-riLocation')]"))).get_attribute("value")
search = driver.find_element_by_xpath("//input[contains(@value, 'Skanna lagerplats')]")
search.send_keys(text)
search.send_keys(Keys.RETURN)
search2 = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//input[contains(@class, 'nl-forms-wp-orange id-requisiteinfo-input-riQuantity')]")))
search.send_keys(Keys.RETURN)
except KeyboardInterrupt:
sys.exit()
def bekräftaSmå():
orange = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//input[contains(@class, 'nl-forms-wp-orange')]")))
try:
while orange:
text = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//input[contains(@class, 'nl-forms-wp-orange id-requisiteinfo-input-riLocation')]"))).get_attribute("value")
search = driver.find_element_by_xpath("//input[contains(@value, 'Skanna lagerplats')]")
search.send_keys(text)
search.send_keys(Keys.RETURN)
search.send_keys(Keys.RETURN)
text = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//input[contains(@class, 'nl-forms-wp-orange')]"))).get_attribute("value")
search2 = driver.find_element_by_xpath("//input[contains(@value, 'Skanna fack')]")
search2.send_keys(text)
search.send_keys(Keys.RETURN)
except KeyboardInterrupt:
sys.exit()
def stopp():
sys.exit()
So I tried in my first code adding import threading
and this block to test it out on (minknapp2)
from tkinter import *
import webreader as wr
import threading
root = Tk()
#Skapa labels
minLabel1 = Label(root, text = "Nyce automation Gui")
minLabel2 = Label(root, text = "Automatisera processer")
minLabel3 = Label(root, text = "Flytta artikel")
minLabel4 = Label(root, text = "Artikelnummer: ")
minLabel5 = Label(root, text = "Från: ")
minLabel6 = Label(root, text = "Till: ")
#Skapa knappar
minKnapp1 = Button(root, text = "Öppna Nyce", command=wr.startaNyce)
minKnapp2 = Button(root, text = "Bekräfta aktiv order", command=threading.Thread(target=wr.bekräfta).start())
But it throws this error.
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Users\Python38-32\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Users\Python38-32\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "\webreader.py", line 17, in bekräfta
orange = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//input[contains(@class, 'nl-forms-wp-orange')]")))
NameError: name 'driver' is not defined
Now I read somewhere that it's because I am starting a thread outside the main program. I am a bit clueless about this part, so any help would be much appreciated.