简体   繁体   中英

How to create a new Tkinter window frame within a window?

So basically I am making a GUI with Tkinter. I also want to make a frame within the Tkinter window, and it should open when I click a button.

Here is my code so far:

from tkinter import *
import tkinter

screen = Tk()
screen.title("My GUI")
screen.geometry("600x600")
screen.configure(background="Gray")

button1 = Button(screen)
button.pack()

screen.mainloop

So how do I make a new window(frame) when I click the button?

You can create/toggle frame following the below logic

from tkinter import *
import tkinter

screen = Tk()
screen.title("My GUI")
screen.geometry("600x600")
screen.configure(background="Gray")

frame_enabled = False


def toggle_frame():
    global frame_enabled
    if not frame_enabled:
        my_frame.pack(fill='both', expand=True)
    else:
        my_frame.pack_forget()
    frame_enabled = not frame_enabled


button1 = Button(screen, text="Toggle frame", command=toggle_frame)
button1.pack()

my_frame = Frame(screen, bg="red")
screen.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