用按钮临时替换整个GUI?

2024-06-13 00:15:26 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使一个tkinterButton暂时完全填满整个gui,然后在按下所说的按钮后,返回到先前的状态。你知道吗

我尝试将按钮设置为我的“最高级别”框架,并使用扩展和填充配置设置,这使得Button非常大,但最终它只填充了我的gui的底部1/3。你知道吗

... other instantiations...

#Initialization of button in gui as whole
toggleBacklightButton = Button(patternOptionFrame,text="Screen Light",
                               font=('calibri',(10)),relief="raised",
                               command=toggleBacklight)
toggleBacklightButton.grid(row=0,column=3)

... other code...

#Function that the button press calls.
def toggleBacklight():
    global backlight_toggle
    backlight_toggle = not backlight_toggle
    if backlight_toggle is True:
        # Button should be as it was when instantiated AND back light
        # is on / all other ~20 widgets are also where they belong.
        os.system(
            "sudo sh -c 'echo \"0\" > /sys/class/backlight/rpi_backlight/bl_power'")
    else:
        # Button should fill entire screen for ease of access when
        # screen is black / all other ~20 widgets are hidden.
        os.system(
            "sudo sh -c 'echo \"1\" > /sys/class/backlight/rpi_backlight/bl_power'")

... other functions...

按钮切换我的触摸屏显示,但是,我不知道如何使它占据整个屏幕时,屏幕背光关闭。你知道吗


Tags: ofisasguibuttonall按钮when
2条回答

Tkinter通常不允许小部件重叠-使你的按钮变大只会将其他小部件推开,实际上它永远不会覆盖它们。在极其罕见的情况下,如果您希望重叠,则只有.place()几何体管理器可以这样做。将按钮设置为窗口本身的直接子级,然后执行以下操作:

toggleBacklightButton.place(x=0, y=0, relwidth=1.0, relheight=1.0)

要让它占据窗户,那么:

toggleBacklightButton.place_forget()

为了摆脱它。你知道吗

如果您想使用重叠的小部件,那么就在一个框架内构建所有内容,并将按钮放置在框架的同一网格位置上。你知道吗

像这样:

import tkinter as tk


root = tk.Tk()

def action():
    btn.destroy()

root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)

frame = tk.Frame(root)
frame.grid(row=0, column=0, sticky="nsew")
tk.Label(frame, text="some random label").pack()

btn = tk.Button(root, text="Some big button", command=action)
btn.grid(row=0, column=0, sticky="nsew")

root.mainloop()

相关问题 更多 >