PythonTkinter循环,用于在输入和离开时更改按钮颜色(Python3.4,Windows 10)

2024-07-04 08:10:58 发布

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

我正在使用Tkinter的“place”功能创建一些按钮。我希望当用户将鼠标悬停在它们上面时,它们显示为灰色,当光标离开时,它们又变回黑色。我可以通过显式地绑定它们来做到这一点,但我想用一个循环动态地绑定它们。我试图创建的循环位于代码的底部,但它不起作用。在

如果您想知道我在说什么,那么这段代码将在python中运行。在

我如何创建一个动态循环来完成这项工作?在

from tkinter import *

FONT_SIZE = 14  # Change this to change all font sizes in the program

root = Tk()

root.resizable(width=TRUE, height=TRUE)
root.geometry("800x600")
root.config(background="black")

# Labels
newGameButton = Label(root)
loadGameButton = Label(root)
optionsButton = Label(root)
exitButton = Label(root)
Labels = [newGameButton, loadGameButton, optionsButton, exitButton]

#Changes the background of buttons when you hover over them
def changeBGEnter(widget):
    widget.config(background="gray")
#Changes the background of buttons when you stop hovering them
def changeBGLeave(widget):
    widget.config(background="black")

# Explicit bindings, ugly
newGameButton.bind("<Enter>", lambda x: changeBGEnter(newGameButton))
newGameButton.bind("<Leave>", lambda x: changeBGLeave(newGameButton))

loadGameButton.bind("<Enter>", lambda x: changeBGEnter(loadGameButton))
loadGameButton.bind("<Leave>", lambda x: changeBGLeave(loadGameButton))

optionsButton.bind("<Enter>", lambda x: changeBGEnter(optionsButton))
optionsButton.bind("<Leave>", lambda x: changeBGLeave(optionsButton))

exitButton.bind("<Enter>", lambda x: changeBGEnter(exitButton))
exitButton.bind("<Leave>", lambda x: changeBGLeave(exitButton))

# New Game Button
newGameButton.config(
    anchor=CENTER,  # Where to anchor the text in the widget
    background="black",
    foreground="white",
    font=("default", FONT_SIZE),
    text="NewGame",
)

newGameButton.place(
    relwidth=0.6,
    relheight=0.1,
    relx=0.2,
    rely=0.3
)

# Load Game Button
loadGameButton.config(
    anchor=CENTER,
    background="black",
    foreground="white",
    font=("default", FONT_SIZE),
    text="Stats",
)

loadGameButton.place(
    relwidth=0.6,
    relheight=0.1,
    relx=0.2,
    rely=0.4
)

# Options Button
optionsButton.config(
    anchor=CENTER,
    background="black",
    foreground="white",
    font=("default", FONT_SIZE),
    text="Output",
)

optionsButton.place(
    relwidth=0.6,
    relheight=0.1,
    relx=0.2,
    rely=0.5
)

# Exit Button
exitButton.config(
    anchor=CENTER,
    background="black",
    foreground="white",
    font=("default", FONT_SIZE),
    text="Exit",
)

exitButton.place(
    relwidth=0.6,
    relheight=0.1,
    relx=0.2,
    rely=0.6
)

#Code I want that doesn't work:
#Get rid of explicit bindings above to test the code below
"""
for x in Labels:
    x.bind("<Enter>", lambda x: changeBGEnter(x))
    x.bind("<Leave>", lambda x: changeBGLeave(x))
"""

root.mainloop()

Tags: thelambdaconfigbindplacerootblackbackground
1条回答
网友
1楼 · 发布于 2024-07-04 08:10:58
  1. 绑定send event data作为lambda中的第一个参数,所以xevent data,而不是标签。

  2. (在for循环中)lambda不会将值从lab复制到changeBGEnter(lab)(而是使用lab作为引用,changeBGEnter的最后一个标签都相同),因此必须使用label=lab

    来复制它

一。在

for lab in Labels:
    lab.bind("<Enter>", lambda event, label=lab: changeBGEnter(label))
    lab.bind("<Leave>", lambda event, label=lab: changeBGLeave(label))

-

(顺便说一句:您可以创建自己的小部件来简化它)


编辑:自己的小部件MyButton,绑定<Enter>和{}

^{pr2}$

编辑:但是您可以使用tkinter.Button来获得相同的结果,而不需要自己的小部件或循环。在

您可以使用command=将函数分配给按钮单击。在

exitButton = Button(root)

def command_exit():
    print "exit"
    root.destroy()

exitButton.config(
    anchor=CENTER,
    font=("default", FONT_SIZE),
    text="Exit",

    # Leave colors
    background="black",
    foreground="white",

    # without border    
    borderwidth=0,
    highlightthickness=0,

    # Enter colors
    activebackground="grey",
    activeforeground="white",

    # run on click
    command=command_exit
)

相关问题 更多 >

    热门问题