我正在製作一個Tkinter Python progarm,它將有一個登錄頁面,登錄後將顯示一個供用戶進行交互的菜單頁面。Tkinter登錄和菜單程序
我的代碼,其中包括測驗類,起始頁類,菜單類,難度類和登錄頁面如下:
import tkinter as tk
from tkinter import *
import tkinter.messagebox as tm
class Quiz (tk.Tk):
def __init__ (self, *args , **kwargs):
tk.Tk.__init__(self, *args , ** kwargs)
container = tk.Frame (self)
container.pack (side = "top" , fill = "both" , expand = True)
container.grid_rowconfigure (0,weight = 1)
container.grid_columnconfigure (0,weight = 1)
self.frames = {}
for F in (StartPage, Menu, Difficulty):
frame = F(container, self)
self.frames[F] = frame
frame.grid (row = 0, column = 0 , sticky = "nsew")
self.show_frame(StartPage)
def show_frame(self,cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text = "Login")
label.pack(pady = 10 , padx = 10)
Username = tk.Entry(self)
Username.pack()
Password = tk.Entry (self, show = "*")
Password.pack()
button1 = tk.Button(self, text = "Login", command = lambda: Login(Username,Password,parent,controller,self))
button1.pack()
class Menu(tk.Frame):
def __init__ (self, parent, controller) :
tk.Frame.__init__(self, parent)
label = tk.Label(self, text = "Menu")
button1 = tk.Button(self, text = "Histoy", command = lambda: controller.show_frame(Difficulty))
button1.pack()
button2 = tk.Button(self, text = "Maths", command = lambda: controller.show_frame(Difficulty))
button2.pack()
button3 = tk.Button(self, text = "Music",
command = lambda: controller.show_frame(Difficulty))
button3.pack()
class Difficulty(tk.Frame):
def __init__ (self, parent, controller):
tk.Frame.__init__(self,parent)
label = tk.Label(self, text = "Difficulty")
Easy = tk.Button(self, text = "Easy", command = lambda: controller.show_frame(Difficulty))
Easy.pack()
Medium = tk.Button(self, text = "Medium",command = lambda: controller.show_frame(Difficulty))
Medium.pack()
Hard = tk.Button(self, text = "Hard", command = lambda: controller.show_frame(Difficulty))
Hard.pack()
backtomenu = tk.Button(self, text = "Back to Menu", command = lambda: controller.show_frame(Menu))
backtomenu.pack()
def Login(Username,Password,parent,controller,self):
Usernames = []
count = 0
Username = Username.get()
Password = Password.get()
try:
with open ("Usernames&Passwords.txt" , "r", encoding = "UTF-8") as file:
for each in file:
Usernames.append(each.strip("\n"))
except IOError as error:
print (error)
if Usernames[count] == Username :
if Usernames[count + 1] == Password:
Menu(parent, controller)
print ("Hi")
else:
tm.showinfo("Username or Password is Incorrect")
else:
tm.showinfo("Username or Password is Incorrect")
app = Quiz()
app.geometry ("500x300")
app.mainloop()
當我運行的代碼沒有顯示錯誤消息,但它不會運行Menu
類,因此它不會轉到下一個Tkinter框架以繼續使用該程序。有人能幫助我指出爲什麼Main類沒有運行的正確方向。提前致謝。
請修復問題中的縮進。這是我們必須繼續。如果你不願花時間來正確地設置代碼的格式,大多數人都不願意花時間給你一個很好的答案。 –