不正確的,Dimiss按鈕,刪除上點擊時,敲擊回車鍵,但如果我以後輸入任何內容,沒有任何反應。問題是,如果我輸入正確,登錄按鈕彈出,它的作品。如果我輸入的不正確,彈出解除按鈕,然後單擊或按下輸入將刪除它。現在,在錯誤的嘗試之後輸入的任何內容無論是正確還是錯誤都無濟於事(1)爲了避免這種情況,我想知道程序是否可以重新啓動,點擊/按下輸入dismiss按鈕,沒有窗戶關閉,或另一個重新打開,但我不知道如何做到這一點。如何點擊按鈕重新啓動tkinter/python程序?
(2)也就是有沒有結束/重新啓動程序,如果這樣我將如何把它放在這個代碼中的最大嘗試登錄的代碼? (有點像,如果> 3不正確然後退出)
下面是代碼(python3) - 自己試試看,如果你想:
from tkinter import *
class Application(object):
def __init__(self, event=None):
self.root = Tk()
self.root.configure(bg="darkorchid1", padx=10, pady=10)
self.root.title("WELCOME")
self.username = "Bob"
self.welcome = Label(self.root, text="WELCOME TO MY PROGRAM", bg="lightgrey", fg="darkorchid1")
self.welcome.pack()
self.label0 = Label(self.root, text="ENTER NAME:", bg="purple", fg="white", height=5, width=50)
self.label0.pack()
self.entry = Entry(self.root, width=25)
self.entry.configure(fg= "white",bg="grey20")
self.entry.pack()
self.entry.bind("<Return>", self.submit)
self.button = Button(self.root, text="SUBMIT", highlightbackground="green", width=48, command=self.submit)
self.button.pack()
def submit(self, event=None):
username = self.entry.get()
if username == self.username:
self.button1 = Button(self.root, text='LOGIN', highlightbackground="green", width=28, command=self.root.destroy)
self.button1.pack()
self.entry.bind("<Return>", self.login)
else:
self.button2 = Button(self.root, text="INCORRECT- CLICK TO DIMISS THIS MESSAGE", highlightbackground="red", width=48, command=self.incorrect)
self.button2.pack()
self.entry.bind("<Return>", self.incorrect)
def incorrect(self, event=None):
self.button2.destroy()
def login(self, event=None):
self.root.destroy()
app=Application()
mainloop()
而不是破壞按鈕,我想這對重新啓動程序但找不到正確的命令。這會破壞按鈕,因爲它在程序開始時不存在,並且允許錯誤或正確的輸入在第一次嘗試後實際工作。
def incorrect(self, event=None):
self.button2.destroy()
我是初學者,所以越簡單越好。謝謝。
你也可以移動的嘗試檢查你創造一個button2(這樣就沒有必要在最後一次嘗試時按下不正確的按鈕) – jross
非常感謝你的時間和幫助。幾個簡單的問題: 爲什麼+ = 1而不是+ 1? 爲什麼將變量設置爲None? 另外我認爲返回只適用於「while」和「for」語句。它也適用於def?它到底在哪裏返回? – gmonz
@gmonz + = 1和說(self.attempts = self.attempts + 1)是一樣的,它只是一個快捷鍵 – jross