如何实现提升方法? [英] How to implement lift method?

查看:22
本文介绍了如何实现提升方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的函数 NEW_FRAME 移动到根窗口的 Frame 前面,以便旧框架和其中的其他小部件将落后于该函数NEW_FRAME 仅显示其小部件.所以我搜索并发现 tkinterlift 方法来实现它,但我无法正确实现它,尝试将它定位在函数内的不同位置.

此链接:

from tkinter import *根 = Tk()root.geometry("1300x600")欢迎 = Frame(root, bg="yellow")Welcome.pack(fill=BOTH, expand=True)label = Label(welcome, text="welcome bro to page one")label.grid(行=45,列=50)b = Label(welcome, text="您可以选择菜单栏切换页面")b.grid(行=100,列=500)def NEWS_Frame():new = Frame(root, bg="red")new.pack(fill=BOTH, expand=True)l1 = Label(new, text="一直在等待")l1.grid(行=49,列=80)l2 = Label(new, text="你好,伙计怎么样")l2.grid(row=0, column=0)new.lift() # 定位它以将新框架提升到框架的顶部# 菜单栏从这里开始MAIN_MENU = 菜单(根)root.config(菜单=MAIN_MENU)File_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()

解决方案

首先,您的最小、完整且可验证的示例 应该包含最少的代码来重现特定问题.下面的代码完全重现了您遇到问题的行为,但仅此而已:

导入 tkinter 作为 tk定义交换():button2.pack()button2.lift()如果 __name__ == '__main__':根 = tk.Tk()button1 = tk.Button(root, text="与按钮 2 交换", command=swap)button2 = tk.Button(root, text="与按钮 1 交换")button1.pack()root.mainloop()

<小时>

使用 pack 的解决方法::强>

不能使用pack将小部件放在另一个小部件上.pack 字面上是用于在 2-d 中堆叠,因为它用于水平或垂直堆叠,但 不是 用于在深度维度上堆叠小部件.然而,一个糟糕的解决方法是在显示另一个小部件时简单地隐藏小部件,这根本不需要lift.

在下面的代码中,每次调用 swap 时,它会隐藏一个按钮,同时显示另一个按钮:

try: # 为了能够导入 tkinter for将 tkinter 作为 tk # 在 python 2 或 python 3 中导入除了:将 Tkinter 作为 tk 导入定义交换():全局 is_button1_lifted如果 is_button1_lifted:button1.pack_forget()button2.pack()别的:button2.pack_forget()button1.pack()is_button1_lifted = 不是 is_button1_lifted如果 __name__ == '__main__':根 = tk.Tk()is_button1_lifted = Truebutton1 = tk.Button(root, text="与按钮 2 交换", command=swap)button2 = tk.Button(root, text="与按钮 1 交换", command=swap)交换()root.mainloop()

<小时>

使用grid回答:

使用的方式lift 在 OP 的情况下.其工作方式是两个小部件都显示在网格的同一个 节点中.小部件 lift 方法用于简单地覆盖其他.

在下面的示例中,两个按钮都显示,而一个(在本例中为button2)只是通过位于另一个前面来阻止另一个.当 lift 被调用时,它只是让它的对象出现在前面:

try: # 为了能够导入 tkinter for将 tkinter 作为 tk # 在 python 2 或 python 3 中导入除了:将 Tkinter 作为 tk 导入定义交换():全局 is_button1_lifted如果 is_button1_lifted:button2.lift()别的:button1.lift()is_button1_lifted = 不是 is_button1_lifted如果 __name__ == '__main__':根 = tk.Tk()is_button1_lifted = Falsebutton1 = tk.Button(root, text="与按钮 2 交换", command=swap)button2 = tk.Button(root, text="与按钮 1 交换", command=swap)button1.grid(row=0, column=0)button2.grid(row=0, column=0)root.mainloop()

<小时>

使用place回答:

这与 grid 的答案几乎相同,place 只是有一个更直接的布局控制:

try: # 为了能够导入 tkinter for将 tkinter 作为 tk # 在 python 2 或 python 3 中导入除了:将 Tkinter 作为 tk 导入定义交换():全局 is_button1_lifted如果 is_button1_lifted:button2.lift()别的:button1.lift()is_button1_lifted = 不是 is_button1_lifted如果 __name__ == '__main__':根 = tk.Tk()is_button1_lifted = Falsebutton1 = tk.Button(root, text="与按钮 2 交换", command=swap)button2 = tk.Button(root, text="与按钮 1 交换", command=swap)button1.place(x=23, y=87)button2.place(x=23, y=87)root.mainloop()

I am trying to move my function NEW_FRAME in front of the Frame for the root window so that old frame and the other widget in it will be behind for the function NEW_FRAME to only display its widget. So I searched and discovered that tkinter has lift method to achieve that but I can't implement it correctly, have tried positioning it at different places inside the function.

this link :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()

解决方案

First of all, your Minimal, Complete, and Verifiable example should only include the least amount of code as possible to reproduce the specific issue. The code below reproduces exactly the behavior you're having an issue with, but nothing more:

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


A workaround using pack:

You can't put widgets over another widget using pack. pack is literally for stacking in the 2-d, as in it is for stacking horizontally or vertically but not for piling widgets in the depth dimension. However, a bad workaround would be to simply hide the widget while displaying the other, which doesn't require lift at all.

In the below code each time swap is called it hides one button while displaying the other:

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


Answer using grid:

This is the way of using lift in the OP's case. The way this works is that both widgets displayed in the same node of a grid. The widget lift method is used on simply comes over the other(s).

In the below example both buttons are displayed, while one(button2 in this case) simply blocks the other by being in front of the other. When the lift is called it simply makes its object come to the front:

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


Answer using place:

This works almost the same way as the answer with grid, place simply has a more direct layout control:

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

这篇关于如何实现提升方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆