2017-05-04 48 views
-3

我使用了一個名爲Thonny的Python程序。它用來製作另一個表示「您已將它變成主應用程序」的框,但我現在刪除了該文本。我希望它顯示一個選項按鈕和一個更改密碼按鈕。這是代碼:Tkinter - 顯示對話框的選項和更改密碼按鈕

import tkinter as tk 

    class Mainframe(tk.Tk): 
     def __init__(self): 
      tk.Tk.__init__(self) 
      self.frame = FirstFrame(self) 
      self.frame.pack() 

     def change(self, frame): 
      self.frame.pack_forget() # delete currrent 
      frame = frame(self) 
      self.frame.pack() # make new frame 

    class FirstFrame(tk.Frame): 
     def __init__(self, master=None, **kwargs): 
      tk.Frame.__init__(self, master, **kwargs) 

      master.title("Enter password") 
      master.geometry("300x200") 

      self.status = tk.Label(self, fg='red') 
      self.status.pack() 
      lbl = tk.Label(self, text='Enter password') 
      lbl.pack() 
      self.pwd = tk.Entry(self, show="*") 
      self.pwd.pack() 
      self.pwd.focus() 
      self.pwd.bind('<Return>', self.check) 
      btn = tk.Button(self, text="Done", command=self.check) 
      btn.pack() 
      btn = tk.Button(self, text="Cancel", command=self.quit) 
      btn.pack() 

     def check(self, event=None): 
      if self.pwd.get() == 'password': 
       self.master.change(SecondFrame) 
      else: 
       self.status.config(text="Wrong password") 

    class SecondFrame(tk.Frame): 
     def __init__(self, master=None, **kwargs): 
      tk.Frame.__init__(self, master, **kwargs) 
      master.title("Main application") 
      master.geometry("600x400") 
     def options_button(self): 
      def set_password(self): 
        e = tk.Entry(master, show="*", textvariable=self.password_set2) 
        e.pack() 
        but1 = tk.Button(self, text="Done", command=self.password_set) 
        but1.pack() 
      b = tk.Button(self, text="Set your password", command=self.set_password) 
      b.pack() 
      c = tk.Button(self, text="Options", command=self.options_button) 
      c.pack() 



    if __name__=="__main__": 
     app=Mainframe() 
     app.mainloop() 

This is What is not working. This is what it originally did

+2

究竟是什麼問題? – WhatsThePoint

+2

請修正縮進,因爲我們實際上並不知道你的代碼是什麼樣的,因爲我們必須移動東西來猜測它可能看起來像 – WhatsThePoint

+0

那麼究竟是什麼問題? – WhatsThePoint

回答

0

所以我有大約一打一點點,我覺得這是你以後有什麼。我也改變了你的代碼,因爲我總覺得最好把所有小部件的盈利放在多類應用上。

import tkinter as tk 

class FirstFrame(tk.Frame): 
    def __init__(self, master, **kwargs): 
     tk.Frame.__init__(self, master, **kwargs) 
     self.pack() 

     master.title("Enter password") 
     master.geometry("300x200") 
     self.status = tk.Label(self, fg='red') 
     self.status.pack() 
     self.lbl = tk.Label(self, text='Enter password') 
     self.lbl.pack() 
     self.pwd = tk.Entry(self, show="*") 
     self.pwd.pack() 
     self.pwd.focus() 
     self.pwd.bind('<Return>', self.check) 
     self.btn = tk.Button(self, text="Done", command=self.check) 
     self.btn.pack() 
     self.btn = tk.Button(self, text="Cancel", command=self.quit) 
     self.btn.pack() 

    def check(self, event=None): 
     if self.pwd.get() == app.password: 
     self.destroy() #destroy current window and open next 
     self.app= SecondFrame(self.master) 
     else: 
     self.status.config(text="Wrong password") 

class SecondFrame(tk.Frame): 
#re organised a lot in here 
    def __init__(self, master, **kwargs): 
     tk.Frame.__init__(self, master, **kwargs) 
     self.pack() 
     master.title("Main application") 
     master.geometry("600x400") 
     self.c = tk.Button(self, text="Options", command=self.options_button) 
     self.c.pack() 
     self.e = tk.Entry(self.master, show="*") 
    def options_button(self): 
     self.e.pack() 
     self.but1 = tk.Button(self, text="Change password", command=self.set_password) 
     self.but1.pack() 
    def set_password(self): 
     app.password=self.e.get() 

if __name__=="__main__": 
    root = tk.Tk() #removed your mainframe class 
    app=FirstFrame(root) 
    #set an attribute of the application for the password 
    #similar to a global variable 
    app.password = "password" 
    root.mainloop() 

因此,與我做了什麼,你得到提示輸入密碼,如果你得到它的權利就轉到下一個卵石,然後一個選項按鈕出現,當點擊它會出現一個輸入框,允許用戶更改密碼

+0

謝謝!它的作品,但更改密碼似乎並沒有改變,但無論如何感謝! – Jake

+0

它確實如果你在'set_password'函數中粘貼'print(app.password)',那麼你會看到它的改變,但是因爲你在主要方法中很難編碼你的密碼,所以每次你的gettting設置爲「password」運行它,以解決您需要在外部存儲和更新的問題 – WhatsThePoint

+0

非常感謝您:-) – Jake