【发布时间】:2021-04-14 08:55:27
【问题描述】:
我用 Python 编写了一个程序,其中有一个 GUI,它显示 4 个按钮(左上右下),当我从键盘按下它们时,它们会返回一条消息。我想要的是,当我按下按钮时,我希望框字段以红色或其他颜色+显示按下哪个按钮的消息。
from tkinter import *
window = Tk()
window.title("Arrow Keys")
window.geometry('820x640')
width = 820
height = 640
myButton1 = Button(window, text=" UP ", activebackground='red')
myButton1.place(x= 387, y=10)
myButton2 = Button(window, text="DOWN", activebackground='red')
myButton2.place(x= 399, y= 60)
myButton2.pack(pady=60)
myButton3 = Button(window, text=" LEFT ", activebackground='red')
myButton3.place(x= 345, y=35)
myButton4 = Button(window, text="RIGHT", activebackground='red')
myButton4.place(x= 435, y= 35)
def up(event):
myLabel = Label(window, text="Press UP")
myLabel.pack()
def down(event):
myLabel = Label(window, text="Press DOWN")
myLabel.pack()
def left(event):
myLabel = Label(window, text="Press LEFT")
myLabel.pack()
def right(event):
myLabel = Label(window, text="Press RIGHT")
myLabel.pack()
window.bind("<Up>", up)
window.bind("<Down>", down)
window.bind("<Left>", left)
window.bind("<Right>", right)
window.mainloop()
【问题讨论】:
-
myButton1.configure(bg='yellow')inup是否满足您的需求?参看。 stackoverflow.com/questions/36093839/… -
谢谢!但是我想当我按下“键盘上的向上箭头”时,GUI 上的 UP 按钮应该亮起一种颜色,给我一个视觉信号,我按下它,当我释放向上箭头时,GUI 上的按钮应该变成灰色再次(禁用)