【问题标题】:Python GUI - How to change a button's colour by pressing a keyboard input?Python GUI - 如何通过按下键盘输入来更改按钮的颜色?
【发布时间】: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') in up 是否满足您的需求?参看。 stackoverflow.com/questions/36093839/…
  • 谢谢!但是我想当我按下“键盘上的向上箭头”时,GUI 上的 UP 按钮应该亮起一种颜色,给我一个视觉信号,我按下它,当我释放向上箭头时,GUI 上的按钮应该变成灰色再次(禁用)

标签: python tkinter


【解决方案1】:

您还需要绑定相应的KeyRelease-* 事件。 例如,添加到您的代码中:

def up(event):
   ...
   myButton1.configure(bg='yellow')

def up_release(event):
   myButton1.configure(bg='grey')

...
window.bind("<KeyRelease-Up>", up_release)

参见例如TkInter keypress, keyrelease events 了解更多详情,How to reset background color of a python tkinter button? 恢复以前的颜色(而不是灰色)。

【讨论】:

    【解决方案2】:
    • 在回调函数外定义状态标签myLabel
    • 定义一个字典来链接键名和对应的按钮
    • &lt;KeyPress&gt;&lt;KeyRelease&gt;绑定到对应的回调中,并在回调中更新对应按钮的背景颜色和myLabel的文字
    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) ### don't call pack() after using place()
    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)
    
    # save the background color or button
    btn_bg = myButton1.cget("bg")
    
    myLabel = Label(window)
    myLabel.place(x=50, y=50)
    
    keys = {"Up": myButton1, "Down": myButton2, "Left": myButton3, "Right": myButton4}
    
    def on_key_press(event):
        if event.keysym in keys:
            keys[event.keysym].config(bg="red")
            myLabel.config(text=event.keysym)
    
    def on_key_release(event):
        if event.keysym in keys:
            keys[event.keysym].config(bg=btn_bg)
            myLabel.config(text="")
    
    window.bind("<KeyPress>", on_key_press)
    window.bind("<KeyRelease>", on_key_release)
    
    window.mainloop()
    

    【讨论】:

      猜你喜欢
      • 2014-03-11
      • 2014-11-05
      • 2014-04-07
      • 1970-01-01
      • 2021-09-15
      • 2016-01-25
      • 2021-02-24
      • 2016-09-11
      • 1970-01-01
      相关资源
      最近更新 更多