简体   繁体   中英

GUI - hiding buttons

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

Wondered if someone could help me basically, with this GUI I am creating I want to be able to access a new page on the same frame but the buttons from the main menu stay once I go to another page, does anyone know how I can hide/forget the buttons and go back to them at a later stage? Thanks.

Updated to use sub-Frames

You could do it using the universal grid_remove() method (here's some documentation ). One way to use it would be to keep references to each of the Button widgets created so you can call this method on them as needed.

However that can be simplified slightly—even though it takes about the same amount of code—by putting all the Buttons s for each page into a separate sub- Frame and just showing or hiding it which will automatically propagate do to all the widgets it contains. This approach also provides a better foundation for the rest of your program.

I've implemented this by adding a main_button_frame attribute to your class, as well as one named student_button_frame to hold those you have on the student page (since you'll probably need it to hide them too).

One nice thing about grid_remove() is that if you later call grid() on the same widget, it will remember all the settings it (and its sub-widgets) had before it was removed, so you don't need to create and maintain a list of every single one of them yourself.

Also note I also made some general modifications to your code so it conforms to the PEP 8 - Style Guide for Python Code recommendations better. I highly recommend you read and start following it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM