我正在用 Python 创建一个基本的登录 GUI。当用户单击“注册新帐户”按钮时,我将如何使它打开一个新窗口?我想为新用户创建一个单独的表单进行注册。需要帮助!
from tkinter import *
from tkinter import messagebox
# pip install pillow
from PIL import Image, ImageTk
class Window(Frame):
def __init__(self, root):
Frame.__init__(self, root)
self.root = root
self.pack(fill=BOTH, expand=1)
self.root.wm_title("Login")
self.root.geometry("1202x632")
self.root.resizable(False,False)
#BG Pic
load = Image.open("books.png")
render = ImageTk.PhotoImage(load)
img = Label(self, image=render)
img.image = render
img.place(x=0, y=0)
#Login Frame
Frame_login=Frame(self.root,bg="white",highlightbackground="#c4652b",highlightthickness=5)
Frame_login.place(x=150,y=150,height=340,width=500)
title=Label(Frame_login,text="Login Here", font=("Impact",35,"bold"),fg="#d77337",bg="white").place(x=72,y=20)
desc=Label(Frame_login,text="Staff Login Area", font=("Goudy old style",15,"bold"),fg="#d25d17",bg="white").place(x=72,y=80)
lbl_user=Label(Frame_login,text="Username", font=("goudy old style",15,"bold"),fg="gray",bg="white").place(x=72,y=110)
self.txt_user=Entry(Frame_login,font=("times new roman",15),bg="lightgray")
self.txt_user.place(x=72,y=140,width=350,height=35)
lbl_passcode=Label(Frame_login,text="Passcode", font=("Goudy old style",15,"bold"),fg="gray",bg="white").place(x=72,y=180)
self.txt_passcode=Entry(Frame_login,font=("times new roman",15),bg="lightgray")
self.txt_passcode.place(x=72,y=208,width=350,height=35)
Register_button=Button(Frame_login,text="Register New Account",bg="white",fg="#d77337",bd=0,font=("times new roman",12)).place(x=72,y=248)
Login_button=Button(self.root,text="Login",fg="white",bg="#d77337",bd=0,font=("times new roman",20,"bold")).place(x=310,y=460,width=180)
root = Tk()
app = Window(root)
root.mainloop()
答案 0 :(得分:1)
使用 command
的 Button()
选项在按钮被点击时执行一个函数。在该函数中,您可以创建另一个窗口,例如创建根窗口:
# sample Toplevel window
class RegisterForm(Toplevel):
def __init__(self, master, *args, **kw):
super().__init__(master, *args, **kw)
self.title("Register")
self.geometry("800x600")
self.resizable(0, 0)
self.config(bg="white")
# other register form widgets
Label(self, text="Registration", font="Impact 32 bold", fg="#c4652b", bg="white").pack()
self.wait_visibility()
self.transient(master)
self.focus_set()
class Window(Frame):
def __init__(self, root):
...
Button(Frame_login,text="Register New Account",...,command=self.register).place(x=72,y=248)
...
def register(self):
RegisterForm(self.root)
答案 1 :(得分:-1)
此页面对您的问题有很好的解释。 enter link description here
一种可能的解决方案:
# This will import all the widgets
# and modules which are available in
# tkinter and ttk module
from tkinter import *
from tkinter.ttk import *
class NewWindow(Toplevel):
def __init__(self, master = None):
super().__init__(master = master)
self.title("New Window")
self.geometry("200x200")
label = Label(self, text ="This is a new Window")
label.pack()
# creates a Tk() object
master = Tk()
# sets the geometry of
# main root window
master.geometry("200x200")
label = Label(master, text ="This is the main window")
label.pack(side = TOP, pady = 10)
# a button widget which will
# open a new window on button click
btn = Button(master,
text ="Click to open a new window")
# Following line will bind click event
# On any click left / right button
# of mouse a new window will be opened
btn.bind("<Button>",
lambda e: NewWindow(master))
btn.pack(pady = 10)
# mainloop, runs infinitely
mainloop()