2017-05-04 85 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

相关问题