from tkinter import *
import tkinter as tk
class dashboard(Frame):
def __init__(self, master):
super(dashboard, self).__init__(master)
self.grid()
self.buttons()
def buttons(self):
#student dashboard button
bttn1 = Button(self, text = "Student",
command=self.student, height = 2, width= 15)
bttn1.grid()
#teacher dashboard button
bttn2 = Button(self, text = "Teacher", height = 2, width= 15)
bttn2.grid()
#exit button
bttn3 = Button(self, text = "Exit",
command=root.destroy, height = 2, width= 15)
bttn3.grid()
def student(self):
#view highscores button
bttn1 = Button(self, text = "Highscores", height = 2, width= 15)
bttn1.grid()
#print score button
bttn2 = Button(self, text = "Print Score", height = 2, width= 15)
bttn2.grid()
#exit button
bttn3 = Button(self, text = "Main Menu",
command=root.destroy, height = 2, width= 15)
bttn3.grid()
#main
root = Tk()
root.title("Dashboard")
root.geometry("300x170")
app = dashboard(root)
root.mainloop()
想知道是否有人可以帮助我基本上,我正在创建这个GUI我希望能够访问同一帧上的新页面,但是一旦我转到另一个页面,主菜单中的按钮就会停留,是否有人知道如何我可以隐藏/忘记按钮并在稍后阶段回复它们?感谢。
答案 0 :(得分:2)
已更新以使用子框架
您可以使用通用grid_remove()
方法(这里有一些documentation)来实现。使用它的一种方法是保持对创建的Button
小部件的每个的引用,以便您可以根据需要在它们上调用此方法。
然而,通过将每个页面的所有Buttons
放入单独的子Frame
并显示或隐藏它,即使它需要大约相同数量的代码,也可以稍微简化一下它会自动将do传播到它包含的所有小部件。这种方法还为您的其他计划提供了更好的基础。
我已经通过在您的课程中添加main_button_frame
属性以及一个名为student_button_frame
的属性来实现此功能,以便在学生页面上保留您的属性(因为您可能需要隐藏它他们也是。)
关于grid_remove()
的一个好处是,如果您稍后在同一个小部件上调用grid()
,它将记住它(及其子小部件)在删除之前的所有设置,因此您不需要自己创建和维护每一个列表。
另请注意,我还对您的代码进行了一些常规修改,以便更好地符合PEP 8 - Style Guide for Python Code建议。我强烈建议您阅读并开始关注它。
from tkinter import *
import tkinter as tk
class Dashboard(Frame):
def __init__(self, master):
super().__init__(master)
self.grid()
self.main_button_frame = None
self.student_button_frame = None
self.create_main_buttons()
def create_main_buttons(self):
if self.student_button_frame: # Exists?
self.student_button_frame.grid_remove() # Make it invisible.
if self.main_button_frame: # Exists?
self.main_button_frame.grid() # Just make it visible.
else: # Otherwise create it.
button_frame = self.main_button_frame = Frame(self)
button_frame.grid()
# Student Dashboard button
bttn1 = Button(button_frame, text="Student",
command=self.create_student_buttons, height=2,
width=15)
bttn1.grid()
# Teacher Dashboard button
bttn2 = Button(button_frame, text="Teacher", height=2, width=15)
bttn2.grid()
# Dashboard Exit button
bttn3 = Button(button_frame, text="Exit", command=root.destroy,
height=2, width=15)
bttn3.grid()
def create_student_buttons(self):
if self.main_button_frame: # Exists?
self.main_button_frame.grid_remove() # Make it invisible.
if self.student_button_frame: # Exists?
student_button_frame.grid() # Just make it visible.
else: # Otherwise create it.
button_frame = self.student_button_frame = Frame(self)
button_frame.grid()
# Highscores button
bttn1 = Button(button_frame, text="Highscores", height=2, width=15)
bttn1.grid()
# Print Score button
bttn2 = Button(button_frame, text="Print Score", height=2, width=15)
bttn2.grid()
# Student Exit button
bttn3 = Button(button_frame, text="Exit", command=root.destroy,
height=2, width=15)
bttn3.grid()
# main
root = Tk()
root.title("Dashboard")
root.geometry("300x170")
app = Dashboard(root)
root.mainloop()