Alex
Alex

Reputation: 43

How to fix "invalid command name" error with Tkinter

solved: as @furas said, I was trying to access email_text after I had already destroyed the window, so email_text no longer existed.

I'm trying to automate a login to a website by grabbing the email and password from an entry using tkinter. The code grabs the email and password and enters it in the site, and succesfully logs in. But I get an error about the email_text.get() line.

Originally I had a prepare() and error() function, so if the login was insuccesful, it would call error() and prompt them to log in again. I had the same error then and wondered if it was an issue with some conflict between widgets in the functions? So I just tried to simplify it into one, but I am still getting the same error. I have 3 different versions of this code and I have been moving bits around, so it's possible that I didnt move something from the other 2 versions that was needed, but I cant figure out which part I could be missing.

from Tkinter import *
from PIL import ImageTk,Image
import time
from datetime import tzinfo
from selenium.webdriver.support.ui import Select

chromedriver = "C:\Users\Alex\Desktop\chromedriver"
driver = webdriver.Chrome(chromedriver)
driver.get("https://www.soundclick.com/community/SC4/login.cfm")

def get_email_pass():
    while True:
        email = email_text.get()
        password = pass_text.get()
        my_window.destroy()
        xpathEmail = '//*[@id="email"]'
        loginEmail = driver.find_element_by_xpath(xpathEmail)
        loginEmail.send_keys(email)
        xpathPass = '//*[@id="password"]'
        loginPass = driver.find_element_by_xpath(xpathPass)
        loginPass.send_keys(password)
        xpathLogin = '//*[@id="loginform"]/div[3]/div[2]/input'
        login = driver.find_element_by_xpath(xpathLogin)
        login.click()
        time.sleep(5)

        if driver.current_url == "https://www.soundclick.com/bandAdmin2/default.cfm?ipv=0":
                exit


#open tkinter window
my_window=Tk()
#Title
my_window.title('SoundClick Login')
#Color
my_window.configure(background="deep sky blue")
#Instr 
email_label=Label(my_window, text="Please Enter The Email Used To Sign Into SoundClick")
email_label.config(background="deep sky blue", foreground="white")
#Create Entry
email_text = Entry(my_window)
email_text.pack()
pass_label=Label(my_window, text="Please Enter The Password Used To Sign Into SoundClick")
pass_label.config(background="deep sky blue", foreground="white")
pass_text=Entry(my_window)
pass_text.pack()


#Censor Password
pass_text.config(show="*")
song_text = Entry(my_window)
song_label=Label(my_window, text="Please Enter The Name Of The Song You Want To Promote. Warning:cAsE SensItIvE")
song_label.config(background="deep sky blue", foreground="white")
#When Done button is pressed, run cmd done_button
finish_button = Button(my_window, text="Done",command=get_email_pass)
finish_button.config(background="white")
note_label=Label(my_window, text="This information will not be stored anywhere")
note_label.config(background="deep sky blue", foreground="white")

#Positioning
email_label.grid(row=0, column=0)
email_text.grid(row=0, column=1)
pass_label.grid(row=1, column=0)
pass_text.grid(row=1, column=1)
finish_button.grid(row=3, column=0)

my_window.mainloop()

The webpage should close if the login is succesful, but instead it stays open and I get this error:

Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1547, in __call__
    return self.func(*args)
  File "C:\Users\Alex\Desktop\Jon.py", line 14, in get_email_pass
    email = email_text.get()
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 2518, in get
    return self.tk.call(self._w, 'get')
TclError: invalid command name ".66283784L"```

Upvotes: 0

Views: 1021

Answers (1)

בנימין כהן
בנימין כהן

Reputation: 739

@furas is right. you are trying to access the contents of the email entry. but on the first iteration, after you are closing the window, you are trying to access it again, but it has closed already!

to fix this you can move the my_window.destroy() from the while True loop to the if statement at the end of the function, so the window will only close after you have connected.

Upvotes: 1

Related Questions