2个按钮重叠Python Tkinter

时间:2017-04-14 22:15:02

标签: python python-3.x user-interface button tkinter

由于赞成票

,此内容已更新为更清晰

我正在Tkinter中创建一个窗口。该窗口包括:

|---------------------------------------------------------------------|
| Element  | Size               | Location          | Function Called |
|----------|--------------------|-------------------|-----------------|
| mButton1 | Width * Height     | 0, 0              | goDown()        |
| mButton2 | Width/8 * Height/8 | Width/8, Height/8 | goUp()          |
|---------------------------------------------------------------------|

mButton1按预期工作,点击后调用我的函数goDown()

mButton2无法正常工作,点击后会调用任何内容。

调试后,似乎有"层"并且mButton1位于覆盖mButton2的顶层,因此无法按下。

我的问题是如何确保mButton2位于mButton1之上,以便在点击时调用该功能?

代码:

import tkinter, sys

root = Tk()
root.geometry("480x320") #Raspberry Pi touchscreen resolution
counter = 30

def goUp():
    counter += 1
    mButton2.config(text = "", borderwidth = 0, highlightthickness=0, relief='ridge', pady = "100")

def downClick():
    counter -= 1
    mButton1.config(text = counter, borderwidth = 0, highlightthickness=0, relief='ridge', pady = "100")

mButton1 = Button(text = counter, command = downClick, height = 4000, width = 320, font = ("Monospace", 200))
mButton1.pack()

mButton2 = Button(text = "", command = downClick, height = 50, width = 50, font = ("Monospace", 10))
mButton2.pack()

root.mainloop()

1 个答案:

答案 0 :(得分:2)

为了让两个按钮同时工作,您可以从较小的尺寸和字体大小开始。

如果您想了解更多关于按钮出现位置的命令,请查看其他几何管理器。 打包受到一定程度的限制,在您的情况下似乎还不够。

对于要重叠的按钮,您可以使用放置管理器:

def upClick():
    global counter
    counter += 1
    mButton1.config(text = counter, borderwidth = 0, highlightthickness=0, relief='ridge', pady = "100")


def downClick():
    global counter
    counter -= 1
    mButton1.config(text = counter, borderwidth = 0, highlightthickness=0, relief='ridge', pady = "100")

mButton1 = Button(text = counter, command = downClick, height = 4000, width = 320, font = ("Monospace", 200))
mButton1.pack()

mButton2 = Button(text = "", command = upClick, height = 5, width = 5, font = ("Monospace", 10))
mButton2.place(anchor="nw")