我正在尝试将NEW_FRAME
的函数移动到根窗口的Frame
前面,以便旧框架和其中的其他窗口小部件将落后于函数NEW_FRAME
只显示其小部件。所以我搜索并发现tkinter
有lift
方法来实现,但我无法正确实现它,尝试将其放置在函数内的不同位置。
此链接:explanation of the lift method with an example
from tkinter import *
root = Tk()
root.geometry("1300x600")
welcome = Frame(root, bg="yellow")
welcome.pack( fill=BOTH, expand=True)
label = Label(welcome, text="welcome bro to page one")
label.grid(row=45, column=50)
b = Label(welcome, text="you can select menu bar to switch page")
b.grid(row=100, column=500)
def NEWS_Frame():
new = Frame(root, bg="red")
new.pack(fill=BOTH, expand=True)
l1 = Label(new, text="Have been waiting for")
l1.grid(row=49, column=80)
l2 = Label(new, text="hello dude how be things")
l2.grid(row=0, column=0)
new.lift() # have position it to lift the new frame to the top of Frame
# menu bar start here
MAIN_MENU = Menu(root)
root.config(menu=MAIN_MENU)
File_menu = Menu(MAIN_MENU)
MAIN_MENU.add_cascade(label="NEW PAGE", menu=File_menu, underline=0)
File_menu.add_command(label="NEWS", command=NEWS_Frame)
root.mainloop()
答案 0 :(得分:1)
lift
在Z轴上移动小部件,pack
在X轴和Y轴上排列小部件。 lift
无法更改使用pack
答案 1 :(得分:0)
首先,您的Minimal, Complete, and Verifiable example应该仅包含最少的数量的代码,以重现特定问题。下面的代码完全重现了您遇到问题的行为,但没有更多:
import tkinter as tk
def swap():
button2.pack()
button2.lift()
if __name__ == '__main__':
root = tk.Tk()
button1 = tk.Button(root, text="Swap with button 2", command=swap)
button2 = tk.Button(root, text="Swap with button 1")
button1.pack()
root.mainloop()
使用pack
的解决方法:
您无法使用pack
将小部件放在另一个小部件上。 pack
实际上是用于在2-d中堆叠,因为它用于水平或垂直堆叠,但不用于深度维度中的堆积小部件。但是,错误的解决方法是在显示另一个时隐藏窗口小部件,根本不需要lift
。
在下面的代码中,每次调用swap
时,它会隐藏一个按钮,同时显示另一个按钮:
try: # In order to be able to import tkinter for
import tkinter as tk # either in python 2 or in python 3
except:
import Tkinter as tk
def swap():
global is_button1_lifted
if is_button1_lifted:
button1.pack_forget()
button2.pack()
else:
button2.pack_forget()
button1.pack()
is_button1_lifted = not is_button1_lifted
if __name__ == '__main__':
root = tk.Tk()
is_button1_lifted = True
button1 = tk.Button(root, text="Swap with button 2", command=swap)
button2 = tk.Button(root, text="Swap with button 1", command=swap)
swap()
root.mainloop()
使用grid
回答:
此 是在OP的情况下使用lift
的方式。这种方式的工作方式是两个小部件都显示在网格的相同的节点中。小部件lift
方法仅用于 over 其他方法。
在下面的示例中,显示了两个按钮,而其中一个按钮(在这种情况下为button2
)仅通过位于另一个按钮之前阻止另一个按钮。当lift
被调用时,它只是让它的对象出现在前面:
try: # In order to be able to import tkinter for
import tkinter as tk # either in python 2 or in python 3
except:
import Tkinter as tk
def swap():
global is_button1_lifted
if is_button1_lifted:
button2.lift()
else:
button1.lift()
is_button1_lifted = not is_button1_lifted
if __name__ == '__main__':
root = tk.Tk()
is_button1_lifted = False
button1 = tk.Button(root, text="Swap with button 2", command=swap)
button2 = tk.Button(root, text="Swap with button 1", command=swap)
button1.grid(row=0, column=0)
button2.grid(row=0, column=0)
root.mainloop()
使用place
回答:
这与grid
的答案几乎相同,place
只有更直接的布局控制:
try: # In order to be able to import tkinter for
import tkinter as tk # either in python 2 or in python 3
except:
import Tkinter as tk
def swap():
global is_button1_lifted
if is_button1_lifted:
button2.lift()
else:
button1.lift()
is_button1_lifted = not is_button1_lifted
if __name__ == '__main__':
root = tk.Tk()
is_button1_lifted = False
button1 = tk.Button(root, text="Swap with button 2", command=swap)
button2 = tk.Button(root, text="Swap with button 1", command=swap)
button1.place(x=23, y=87)
button2.place(x=23, y=87)
root.mainloop()