如何用Tkin修复“无效命令名”错误

2024-06-02 08:12:23 发布

您现在位置:Python中文网/ 问答频道 /正文

已解决: 正如@furas所说,在我破坏了窗口之后,我试图访问电子邮件文本,因此电子邮件文本不再存在

我试图通过使用tkinter从一个条目中获取电子邮件和密码来自动登录到一个网站。代码获取电子邮件和密码并将其输入站点,然后成功登录。但是我得到一个关于email\u text.get()行的错误

最初我有一个prepare()和error()函数,因此如果登录失败,它将调用error()并提示他们再次登录。我当时也犯了同样的错误,我想知道这是否是函数中的小部件之间存在冲突的问题?所以我试着把它简化成一个,但是我仍然得到同样的错误。我有3个不同版本的代码,我一直在移动位,所以有可能我没有移动的东西,从其他2个版本是需要的,但我不知道哪一部分我可能会丢失

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()

如果登录成功,该网页应关闭,但它将保持打开状态,并且出现以下错误:

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"```


Tags: textimportconfiggetemailmydriverblue
1条回答
网友
1楼 · 发布于 2024-06-02 08:12:23

@furas是对的。您正在尝试访问email项的内容。但是在第一次迭代中,在您关闭窗口之后,您试图再次访问它,但是它已经关闭了

要解决这个问题,您可以将my_window.destroy()while True循环移到函数末尾的if语句,这样窗口只有在连接之后才会关闭

相关问题 更多 >