【问题标题】:How to fix "invalid command name" error with Tkinter如何使用 Tkinter 修复“无效的命令名称”错误
【发布时间】:2019-06-26 00:55:44
【问题描述】:

解决了: 正如@furas 所说,我在销毁窗口后尝试访问 email_text,因此 email_text 不再存在。

我正在尝试通过使用 tkinter 从条目中获取电子邮件和密码来自动登录网站。该代码获取电子邮件和密码并将其输入站点,并成功登录。但是我收到有关 email_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"```

【问题讨论】:

  • 您的问题可能是 while True 在 get_email_pass() 内运行多次 email_text.get() - 但在运行 email_text.get() 第一次运行 my_window.destroy() 之后,email_text 在 @ 时不存在987654329@ 第二次运行email_text.get()。这会给出错误“无效的命令名称”

标签: python python-2.7 tkinter


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    • 2013-07-07
    • 1970-01-01
    • 2017-10-04
    • 1970-01-01
    • 2021-05-15
    相关资源
    最近更新 更多