帧交换不起作用

问题描述 投票:0回答:1

我想切换框架,但不能这样做

第一页(框架)应具有红色背景颜色和“Hello”按钮,框架尺寸应为900x650作为窗口大小。按“Hello”按钮时,它应交换到第2帧

第2页(框架)应具有绿色背景颜色和“Hello”按钮,框架尺寸应为900x650作为窗口大小。按“Hello”按钮时,它应交换到第1帧

import Tkinter as tk

def raise_frame(frame):
    print "Inside raise frame"
    frame.tkraise()

root = tk.Tk()
root.geometry("900x650+220+20")
root.title("Testing")


frame1 = tk.Frame(root, width=900, height=650, background="red")
frame2 = tk.Frame(root, width=900, height=650, background="green")


B1= tk.Button(frame1, text="Hello", width =10, height=2, command = lambda:raise_frame(frame2)).place (x=200, y=200)
B2= tk.Button(frame2, text="Hello", width =10, height=2, command = lambda:raise_frame(frame1)).place (x=400, y=400)



frame1.pack( )
frame2.pack( )

root.mainloop()
python tkinter
1个回答
2
投票

由于您使用的是pack(),因此第二帧位于第一帧下方。您可以通过拖动窗口的底部来检查。您将看到顶部有红色,底部有绿色,有2个框架。

您可以使用grid()将帧放在彼此的顶部。

所以,更换线

frame1.pack()
frame2.pack()

frame1.grid(row=0, column=0)
frame2.grid(row=0, column=0)
© www.soinside.com 2019 - 2024. All rights reserved.