【问题标题】:Tkinter - show dialog with options and change password buttonsTkinter - 显示带有选项和更改密码按钮的对话框
【发布时间】:2017-10-02 14:22:54
【问题描述】:

我使用一个名为 Thonny 的 Python 程序。它曾经在里面制作另一个框,上面写着“您已将其放入主应用程序”,但我现在删除了那段文字。我希望它显示一个选项按钮和一个更改密码按钮。这是代码:

    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

【问题讨论】:

  • 问题到底是什么?
  • 请按原样修复缩进,我们实际上并不知道您的正确代码是什么样的,因为我们必须四处移动以猜测它可能是什么样的
  • 那么问题到底是什么?
  • 选项按钮和设置的密码应该会出现,但不会出现。在python中尝试一下,看看。我使用 thonny,它适用于此。

标签: python tkinter tk


【解决方案1】:

所以我玩了一些游戏,我认为这就是你想要的。我还稍微更改了您的代码,因为我总是觉得最好将 self 放在多类应用程序的所有小部件的前面。

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()

因此,按照我所做的操作,系统会提示您输入密码,如果输入正确,则会进入下一个屏幕,然后会出现一个选项按钮,如果单击它,则会出现一个输入框,允许用户更改密码

【讨论】:

  • 谢谢!它有效,但更改密码似乎没有改变,但还是谢谢!
  • 确实如此当你运行它时,为了解决这个问题,你需要在外部存储和更新它
  • 如果这对您有用,我可以请您接受它作为答案吗?因为这对我有很大帮助
  • 我还有一个问题,我已经制作了另一个文件并添加了一个名为password_store的变量我希望在当前密码存储所在的位置调用它。
  • 如果你的 password_store 是一个文本文件或类似文件,你可以从文件中读取它,但如果它是另一个 python 文件,那么你需要导入值
猜你喜欢
  • 1970-01-01
  • 2014-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多