簡體   English   中英

Python-如何在Tkinter窗口中切換幀

[英]Python - How to switch frames in a Tkinter window

我想在Tkinter窗口中切換幀,但是上一幀始終在后台保持可見狀態。

這是我的代碼:

from tkinter import *

class frame(Frame):

    def __init__(self,display):
        Frame.__init__(self,display)
        l = Label(self,text="frame1")
        l.pack()

class frame2(Frame):

    def __init__(self,display):
        Frame.__init__(self,display)
        l = Label(self,text="frame2")
        l.pack()   

class test(Tk):

    def __init__(self):
        Tk.__init__(self)
        f = frame(self)
        f.grid(row=0)
        f2 = frame2(self)
        f2.grid(row=0)
        f.tkraise()

t = test()
t.mainloop()

如果兩個框架的布局相同,但是如果我在第二個框架中添加另一個標簽,則該標簽仍將在背景中可見,這將起作用。 有沒有一種方法可以切換框架,以便僅顯示凸起框架中的元素?

根據要求,這是我用來解決問題的方法:

from tkinter import *

class frame(Frame):

    def __init__(self,display):
        Frame.__init__(self,display)
        l = Label(self,text="frame1")
        l.pack()

class frame2(Frame):

    def __init__(self,display):
        Frame.__init__(self,display)
        l = Label(self,text="frame2")
        l.pack()   

class test(Tk):

    def __init__(self):
        Tk.__init__(self)
        f2 = frame2(self)
        f2.grid(row=0)

        #To raise the first frame, I used the following
        frame2.grid_remove()
        f = frame(self)
        f.grid(row=0)

t = test()
t.mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM