GUI隐藏按钮

2024-06-25 23:06:01 发布

您现在位置:Python中文网/ 问答频道 /正文

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()

想知道是否有人可以帮助我基本上,有了这个图形用户界面,我想能够访问一个新的页面在同一个框架,但从主菜单的按钮,一旦我进入另一个页面,谁知道我可以隐藏/忘记按钮,然后在稍后的阶段回到他们?谢谢。在


Tags: textselfdefbuttonrootwidthstudentcommand
1条回答
网友
1楼 · 发布于 2024-06-25 23:06:01

更新为使用子帧

您可以使用通用的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()

相关问题 更多 >