我只是想知道我犯了什么错误,这意味着我的GUI无法正常工作。它的意思是打开一个带有3个按钮的窗口,当你点击这些按钮时,它会打开一个新的GUI - 显然对于Customer注册,它会打开一个注册页面。谢谢!
import tkinter as tk
class MainWindow:
def __init__(self, master):
self.master = master
self.frame = tk.Frame(self.master)
self.button1 = tk.Button(self.frame, text = "Customer login", width = 25, command = self.new_window)
self.button1.pack()
self.button2 = tk.Button(self.frame, text = "Customer register", width = 25, command = self.new_window2)
self.button2.pack()
self.button3 = tk.Button(self.frame, text = "Driver login", width = 25, command = self.new_window3)
self.button3.pack()
self.frame.pack()
def new_window(self):
self.newWindow = tk.Toplevel(self.master)
self.app = Login(self.newWindow)
def new_window2(self):
self.newWindow2 = tk.Toplevel(self.master)
self.app2 = CustReg(self.newWindow2)
def new_window3(self):
self.newWindow3 = tk.Toplevel(self.master)
self.app3 = DriverLog(self.newWindow3)
class Login:
def __init__(self, master):
self.master = master
self.frame = tk.Frame(self.master)
self.newL = tk.Label(self.master, text = "username", height = 1, width = 25)
self.newL.pack()
self.newT = tk.Text(self.master, height = 1, width = 25)
self.newT.pack()
self.secL = tk.Label(self.master, text = "Password", height = 1, width = 25)
self.secL.pack()
self.secT = tk.Text(self.master, height = 1, width = 25)
self.secT.pack()
self.quitButton = tk.Button(self.frame, text = 'Log in', width = 25, command = self.master.destroy)
self.quitButton.pack()
self.frame.pack()
class CustReg:
def __init__(self, master):
self.master = master
self.frame = tk.Frame(self.master)
self.Label = tk.Label(self.master, text = "Enter your desired username", height = 1, width = 25)
self.Label.pack()
self.TextU = tk.Text(self.master, height = 1, width = 25)
self.TextU.pack()
self.Label2 = tk.Label(self.master, text = "Enter your desired password", height = 1, width = 25)
self.Label2.pack()
self.TextP = tk.Text(self.master, height = 1, width = 25)
self.TextP.pack()
self.Label3 = tk.Label(self.master, text = "Enter your email address", height = 1, width = 25)
self.Label3.pack()
self.TextE = tk.Tex(self.master, height = 1, width = 25)
self.TextE.pack()
self.Label4 = tk.Label(self.master, text = "Enter your pick up address", height = 1, width = 25)
self.Label4.pack()
self.TextPick = tk.Text(self.master, height = 1, width = 25)
self.TextPick.pack()
self.frame.pack()
class DriverLog:
def __init__(self, master):
self.master = master
self.frame = tk.Frame(self.master)
self.gg = tk.Button(self.master, text = "Log in", height = 2, width = 25, command = self.master.destroy)
self.gg.pack()
def main():
root = tk.Tk()
app = Login(root)
app2 = CustReg(root)
app3 = DriverLog(root)
root.mainloop()
if __name__ == '__main__':
main()
答案 0 :(得分:1)
不确定您要实现的目标,主方法中的代码如何
def main():
root = tk.Tk()
app = MainWindow(root)
root.mainloop()
它似乎更接近你想要实现的目标。它打开一个窗口,第一个按钮打开登录窗口,第二个打开客户注册窗口,第三个打开另一个登录窗口(驱动程序登录)
完整代码
import tkinter as tk
class MainWindow:
def __init__(self, master):
self.master = master
self.frame = tk.Frame(self.master)
self.button1 = tk.Button(self.frame, text = "Customer login", width = 25, command = self.new_window)
self.button1.pack()
self.button2 = tk.Button(self.frame, text = "Customer register", width = 25, command = self.new_window2)
self.button2.pack()
self.button3 = tk.Button(self.frame, text = "Driver login", width = 25, command = self.new_window3)
self.button3.pack()
self.frame.pack()
def new_window(self):
self.newWindow = tk.Toplevel(self.master)
self.app = Login(self.newWindow)
def new_window2(self):
self.newWindow2 = tk.Toplevel(self.master)
self.app2 = CustReg(self.newWindow2)
def new_window3(self):
self.newWindow3 = tk.Toplevel(self.master)
self.app3 = DriverLog(self.newWindow3)
class Login:
def __init__(self, master):
self.master = master
self.frame = tk.Frame(self.master)
self.newL = tk.Label(self.master, text = "username", height = 1, width = 25)
self.newL.pack()
self.newT = tk.Text(self.master, height = 1, width = 25)
self.newT.pack()
self.secL = tk.Label(self.master, text = "Password", height = 1, width = 25)
self.secL.pack()
self.secT = tk.Text(self.master, height = 1, width = 25)
self.secT.pack()
self.quitButton = tk.Button(self.frame, text = 'Log in', width = 25, command = self.master.destroy)
self.quitButton.pack()
self.frame.pack()
class CustReg:
def __init__(self, master):
self.master = master
self.frame = tk.Frame(self.master)
self.Label = tk.Label(self.master, text = "Enter your desired username", height = 1, width = 25)
self.Label.pack()
self.TextU = tk.Text(self.master, height = 1, width = 25)
self.TextU.pack()
self.Label2 = tk.Label(self.master, text = "Enter your desired password", height = 1, width = 25)
self.Label2.pack()
self.TextP = tk.Text(self.master, height = 1, width = 25)
self.TextP.pack()
self.Label3 = tk.Label(self.master, text = "Enter your email address", height = 1, width = 25)
self.Label3.pack()
self.TextE = tk.Text(self.master, height = 1, width = 25)
self.TextE.pack()
self.Label4 = tk.Label(self.master, text = "Enter your pick up address", height = 1, width = 25)
self.Label4.pack()
self.TextPick = tk.Text(self.master, height = 1, width = 25)
self.TextPick.pack()
self.frame.pack()
class DriverLog:
def __init__(self, master):
self.master = master
self.frame = tk.Frame(self.master)
self.gg = tk.Button(self.master, text = "Log in", height = 2, width = 25, command = self.master.destroy)
self.gg.pack()
def main():
root = tk.Tk()
app = MainWindow(root)
root.mainloop()
if __name__ == '__main__':
main()