2017-03-11 152 views
2

不正確的,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() 

我是初學者,所以越簡單越好。謝謝。

回答

1

首先我會改變綁定返回到根窗口,而不是條目(否則你必須在輸入欄點擊返回有效果)

接下來,你需要3個狀態變量爲你的班級。

self.button1 = None 
self.button2 = None 
self.attempts = 0 

然後通過檢查每個國家可以完成(我覺得)你想

這裏是整個代碼改性

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.root.bind("<Return>", self.submit) 

     self.button = Button(self.root, text="SUBMIT", highlightbackground="green", width=48, command=self.submit) 
     self.button.pack() 

     self.button1 = None 
     self.button2 = None 
     self.attempts = 0 

    def submit(self, event=None): 
     username = self.entry.get() 
     if username == self.username: 
      if (self.button2 != None): # after I added disabling the submit button this check might not be necessary 
       return 
      if (self.button1 == None): 
       self.button1 = Button(self.root, text='LOGIN', highlightbackground="green", width=28, command=self.root.destroy) 
       self.button1.pack() 
       self.root.bind("<Return>", self.login) 
       self.button.config(state="disabled") 
     else: 
      if (self.button2 == None): 
       self.button2 = Button(self.root, text="INCORRECT- CLICK TO DIMISS THIS MESSAGE", highlightbackground="red", width=48, command=self.incorrect) 
       self.button2.pack() 
       self.root.bind("<Return>", self.incorrect) 
       self.button.config(state="disabled") 

    def incorrect(self, event=None): 
     self.attempts += 1 
     if (self.attempts > 2): 
      self.root.destroy() 
     else: 
      self.root.bind("<Return>", self.submit) 
      self.button.config(state="normal") 
      self.button2.destroy() 
      self.button2 = None 

    def login(self, event=None): 
     self.root.destroy() 

app=Application() 

mainloop() 
+0

你也可以移動的嘗試檢查你創造一個button2(這樣就沒有必要在最後一次嘗試時按下不正確的按鈕) – jross

+0

非常感謝你的時間和幫助。幾個簡單的問題: 爲什麼+ = 1而不是+ 1? 爲什麼將變量設置爲None? 另外我認爲返回只適用於「while」和「for」語句。它也適用於def?它到底在哪裏返回? – gmonz

+0

@gmonz + = 1和說(self.attempts = self.attempts + 1)是一樣的,它只是一個快捷鍵 – jross

2

不是專家,但我自己前一段時間與使用TKinter撥弄。從我知道的重新啓動TKinter應用程序的唯一方法是通過在一個線程中運行它,然後殺死並重新啓動該線程。我建議你看一看Python的multiprocessingthreading模塊。

你也可以嘗試一些東西(在過去對我有用,但並不是真正做到這一點的正確方法,我想)是將ROOT = Tk()作爲一個全局變量,然後將你的提交按鈕作爲一個立場 - 單獨的類,並讓它執行ROOT.Destroy()之前導入global ROOT,然後有它再次調用應用程序類,也可以調用全局變量(這將導致它重新開始)。這是我用來在我使用TKinter窗口時更新的方法。但是,線程方法經常被提及作爲這樣做的正確方法,據我所知..

作爲一個非常有限的例子(對於更廣泛的一個時間很短),我在我的舊腳本中做了這樣的事情:

ROOT_WINDOW = "" 
VARIABLE1 = 'some variable here' 

def func1(): 

    global ROOT_WINDOW 
    global VARIABLE1 
    ROOT_WINDOW = Tk() 
    #(Insert application code that uses or requires variable1) 
    func2() 

def func2(): 
    global ROOT_WINDOW 
    global VARIABLE1 
    ROOT_WINDOW.Destroy() 
    Change variable1 
    func1() 

關於設定登錄嘗試的最大數量。我通過創建獨立的啓動腳本解決了這個問題。它工作正常,但我不敢在這裏發佈它,因爲它是一個真正的醜陋和不正確的方法來解決也帶有安全問題(不得不將你的sudo密碼存儲在變量中)的問題。

對不起,不能夠有更多的幫助。

相關問題