簡體   English   中英

如何使用Tkinter修復“無效的命令名稱”錯誤

[英]How to fix “invalid command name” error with Tkinter

解決:正如@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"```

@furas是正確的。 您正在嘗試訪問email條目的內容。 但是在第一次迭代中,關閉窗口后,您嘗試再次訪問它,但是它已經關閉了!

要解決此問題,您可以將my_window.destroy()while True循環移至函數末尾的if語句,因此僅在連接后窗口才會關閉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM