如何让tkinter中的一个按钮更新到其他的显示页面?

0 人关注

我在做一个登录系统,所以我做了一个有两个按钮的显示屏,分别是登录和注册按钮,所以我想在用户点击其中一个按钮的时候出现一个新的页面,例如,如果他点击了登录,这个页面就会更新为一个有证书输入框的页面,同样的,注册按钮也是如此,我需要一些想法,我是Tkinter的初学者,下面是我的代码。

from tkinter import *
root = Tk()
root.title("Login System")
root.geometry('550x300')
root.configure(bg="Black")
home_label = Label(root,text="Welcome",font=("Helvatica",32),fg="White",bg="Black")
home_label.pack(pady=30)
login = Button(root,text="Login",font=("Nexa",22),bg="White",fg="Black",width=10)
login.pack(pady=0)
signup = Button(root,text="Sign UP",font=("Nexa",22),bg="White",fg="Black",width=10)
signup.pack(pady=5)
root.mainloop()
    
3 个评论
你可以做的是创建一个 TopLevel() 。或者,你可以把所有的主要元素添加到框架中,然后改变框架。或者,你可以告诉几何管理器,忘记放置
@Sujay 你能不能提供一个示例代码?无论如何,谢谢你。
是的,请稍等片刻。
python
user-interface
tkinter
Colton Walker
Colton Walker
发布于 2021-06-12
2 个回答
user15801675
发布于 2021-06-12
已采纳
0 人赞同

这里有一个简单的 TopLevel()

import tkinter as tk
from tkinter.simpledialog import askstring
from tkinter import *
import os
import re
#Designing window for registration
def register():
    global register_screen
    register_screen = Toplevel(main_screen)
    register_screen.title("Register")
    register_screen.geometry("300x250")
#Globalling variables
    global username
    global password
    global username_entry
    global password_entry
    global invLbl
    username = StringVar()
    password = StringVar()
 #Labels and entry fields for register screen
    invLbl = Label(register_screen, text="", fg="red", font=("calibri", 11))
    Label(register_screen, text="Please enter details below", bg="blue").pack()
    Label(register_screen, text="").pack()
    username_lable = Label(register_screen, text="Username * ")
    username_lable.pack()
    username_entry = Entry(register_screen, textvariable=username)
    username_entry.pack()
    password_lable = Label(register_screen, text="Password * ")
    password_lable.pack()
    password_entry = Entry(register_screen, textvariable=password, show='*')
    password_entry.pack()
    Label(register_screen, text="").pack()
    Button(register_screen, text="Register", width=10, height=1, bg="blue", command = register_user).pack()
    invLbl.pack()
#Designing window for login
def login():
    global login_screen
    login_screen = Toplevel(main_screen)
    login_screen.title("Login")
    login_screen.geometry("300x250")
    Label(login_screen, text="Please enter details below to login").pack()
    Label(login_screen, text="").pack()
    global username_verify
    global password_verify
    username_verify = StringVar()
    password_verify = StringVar()
    global username_login_entry
    global password_login_entry
    Label(login_screen, text="Username * ").pack()
    username_login_entry = Entry(login_screen, textvariable=username_verify)
    username_login_entry.pack()
    Label(login_screen, text="").pack()
    Label(login_screen, text="Password * ").pack()
    password_login_entry = Entry(login_screen, textvariable=password_verify, show= '*')
    password_login_entry.pack()
    Label(login_screen, text="").pack()
    Button(login_screen, text="Login", width=10, height=1, command = login_verify).pack()
#Implementing event on register button
def register_user():    
    username_info = username.get()
    password_info = password.get()
    valid = True
    for char in username_info:
        if char.isdigit() == True:
            valid = False
            #If there is a for example a number iin the username, this error message will be printed.
            invLbl.config(text="Invalid Username")
    if username_info == '':
        valid = False
        #If the username is left blank, this error message will be printed.
        invLbl.config(text="Enter a Username")
    #Password Validation
    if len(password_info) <3 or len(password_info) > 16:
        valid = False
        invLbl.config(text="Ensure password is not less than 3 or greater than 16 digits.")
    if valid == True:
        file = open(username_info, "w")
        file.write(username_info + "\n")
        file.write(password_info)
        file.close()
        #Registration Success Message
        invLbl.config(text="Registration Success!", fg="green")
    username_entry.delete(0, END)
    password_entry.delete(0, END)
#Implementing event on login button
def login_verify():
    username1 = username_verify.get()
    password1 = password_verify.get()
    username_login_entry.delete(0, END)
    password_login_entry.delete(0, END)
    list_of_files = os.listdir()
    if username1 in list_of_files:
        file1 = open(username1, "r")
        verify = file1.read().splitlines()
        if password1 in verify:
            login_sucess()
        else:
            password_not_recognised()
    else:
        user_not_found()
#Designing popup for login success
def login_sucess():
    global login_success_screen
    login_success_screen = Toplevel(login_screen)
    login_success_screen.title("Success")
    login_success_screen.geometry("150x100")
    Label(login_success_screen, text="Login Success").pack()
    Button(login_success_screen, text="OK", command=delete_login_success).pack()
#Designing popup for login invalid password
def password_not_recognised():
    global password_not_recog_screen
    password_not_recog_screen = Toplevel(login_screen)
    password_not_recog_screen.title("Success")
    password_not_recog_screen.geometry("150x100")
    Label(password_not_recog_screen, text="Invalid Password ").pack()
    Button(password_not_recog_screen, text="OK", command=delete_password_not_recognised).pack()
#Designing popup for user not found
def user_not_found():
    global user_not_found_screen
    user_not_found_screen = Toplevel(login_screen)
    user_not_found_screen.title("Success")
    user_not_found_screen.geometry("150x100")
    Label(user_not_found_screen, text="User Not Found").pack()
    Button(user_not_found_screen, text="OK", command=delete_user_not_found_screen).pack()
#Deleting popups
def delete_login_success():
    login_success_screen.destroy()
    login_screen.destroy()
def delete_password_not_recognised():
    password_not_recog_screen.destroy()
def delete_user_not_found_screen():
    user_not_found_screen.destroy()
#Designing Main(first) window
def main_account_screen():
    global main_screen
    main_screen = Tk()
    main_screen.geometry("300x250")
    main_screen.title("Account Login")
    Label(text="Select Your Choice", bg="blue", width="300", height="2", font=("Calibri", 13)).pack()
    Label(text="").pack()
    Button(text="Login", height="2", width="30", command = login).pack()
    Label(text="").pack()
    Button(text="Register", height="2", width="30", command=register).pack()
    main_screen.mainloop()
main_account_screen()
    
我想我现在完全知道该怎么做了,非常感谢您,先生,您非常有帮助。
Ata Reenes
Ata Reenes
发布于 2021-06-12
0 人赞同

我想我们可以用不同的方法来处理这个问题,其中一个方法是创建一个框架,并在使用按钮时销毁它,另一个方法是合并不同的页面并使用这个按钮在它们之间导航。

import Tkinter as tk
class Page(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
    def show(self):
        self.lift()
class Page1(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, text="This is page 1")
       label.pack(side="top", fill="both", expand=True)
class Page2(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, text="This is page 2")
       label.pack(side="top", fill="both", expand=True)
class Page3(Page):
   def __init__(self, *args, **kwargs):
       Page.__init__(self, *args, **kwargs)
       label = tk.Label(self, text="This is page 3")
       label.pack(side="top", fill="both", expand=True)
class MainView(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        p1 = Page1(self)
        p2 = Page2(self)
        p3 = Page3(self)
        buttonframe = tk.Frame(self)
        container = tk.Frame(self)
        buttonframe.pack(side="top", fill="x", expand=False)
        container.pack(side="top", fill="both", expand=True)
        p1.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        p2.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        p3.place(in_=container, x=0, y=0, relwidth=1, relheight=1)
        b1 = tk.Button(buttonframe, text="Page 1", command=p1.show)
        b2 = tk.Button(buttonframe, text="Page 2", command=p2.show)
        b3 = tk.Button(buttonframe, text="Page 3", command=p3.show)
        b1.pack(side="left")
        b2.pack(side="left")
        b3.pack(side="left")
        p1.show()
if __name__ == "__main__":
    root = tk.Tk()
    main = MainView(root)