简体   繁体   English

如何在 python 中通过箭头键更改按钮选择

[英]How to change button selection by arrow keys in python

I am using tkinter library in python. I have made a Button.我在python中使用tkinter库。我做了一个按钮。 I want to change that highlighted Button to next Button by arrow key.我想通过箭头键将突出显示的按钮更改为下一个按钮。 Here is my code.这是我的代码。 Thank you for any help感谢您的任何帮助

from tkinter import*

w = Tk()
w.title("Virtual Message")
w.configure(bg="#e6e8b5")
w.option_add("*Font", "consolas 40",)

def bttn(x, y, text, cmd, img, wid, hei):
    def on_enter(e):
        print(mybutton)
        mybutton['background'] = "green"
        mybutton['foreground'] = "orange"

    def on_leave(e):
        mybutton['background'] = "orange"
        mybutton['foreground'] = "green"

    mybutton = Button(w, width=wid, height=hei, text=text,
                      fg="green", bg= "orange", border=0, image=img, pady=20, bd=5,
                      activeforeground="green",
                      activebackground="orange", command=cmd, compound=TOP)

    mybutton.bind("<Enter>", on_enter)
    mybutton.bind("<Leave>", on_leave)
    mybutton.place(x=x, y=y)

photo1 = PhotoImage(file="picture/food.png")
photo2 = PhotoImage(file="picture/shower.png")
photo3 = PhotoImage(file="picture/toilet.png")
photo4 = PhotoImage(file="picture/tv.png")
photo5 = PhotoImage(file="picture/monk.png")
photo6 = PhotoImage(file="picture/music.png")
photo7 = PhotoImage(file="picture/sos.png")

bttn(50, 50, "หิว", setup_lineNotify1, photo1, 375, 300)
bttn(50, 500, "อาบน้ำ", setup_lineNotify2, photo2, 375, 300)
bttn(458.25, 50, "ขับถ่าย", setup_lineNotify3, photo3, 375, 300)
bttn(458.25, 500, "โทรทัศน์", setup_lineNotify4, photo4, 375, 300)
bttn(866.5, 50, "สวดมนต์", setup_lineNotify5, photo5, 375, 300)
bttn(866.5, 500, "ฟังเพลง", setup_lineNotify6, photo6, 375, 300)
bttn(1275, 50, "ช่วยเหลือ", setup_lineNotify7, photo7, 375, 750)

def main():
    w.mainloop()

if __name__ == "__main__":
    main()

This Picture when I select button by mouse This Picture 当我用鼠标点击 select 时

enter image description here在此处输入图像描述

You could bind the buttons to <Left> , <Right> , <Up> and <Down> events.您可以将按钮绑定到<Left><Right><Up><Down>事件。 To set focus to a particular button, use <button-widget>.focus_set() .要将焦点设置到特定按钮,请使用<button-widget>.focus_set()

Considering the code you have given, you could store all the button widgets in a sequence and then add the bindings depending on how exactly you want to switch from one button to another.考虑到您提供的代码,您可以按顺序存储所有按钮小部件,然后根据您想要从一个按钮切换到另一个按钮的精确程度添加绑定。

Also, if the container widget of a button is not the main window, you need to use the .focus_set() method on the container widget as well.此外,如果按钮的容器小部件不是主要的 window,则还需要在容器小部件上使用.focus_set()方法。

Illustrative Code:说明代码:

from tkinter import *

def on_right(e):
    global button2
    button2.focus_set()
    

def on_left(e):
    global button1
    button1.focus_set()

root = Tk()

button1 = Button(root, text = "Button1", font = ('Comic Sans', 15, 'bold'), bg = "white", fg = "black")
button1.grid(row = 0, column = 0, sticky = NSEW, ipadx = 5, ipady = 2, padx = 5, pady = 5)
button1.bind("<Right>", on_right)
button1.bind("<FocusIn>", lambda e: button1.configure(bg = "black", fg = "white"))
button1.bind("<FocusOut>", lambda e: button1.configure(bg = "white", fg = "black"))
button1.focus_set() #Setting default focus to button1

button2 = Button(root, text = "Button2", font = ('Comic Sans', 15, 'bold'), bg = "white", fg = "black")
button2.grid(row = 0, column = 1, sticky = NSEW, ipadx = 5, ipady = 2, padx = 5, pady = 5)
button2.bind("<FocusIn>", lambda e: button2.configure(bg = "black", fg = "white"))
button2.bind("<FocusOut>", lambda e: button2.configure(bg = "white", fg = "black"))
button2.bind("<Left>", on_left)

root.mainloop()

Button1 has focus Button1 有焦点

在此处输入图像描述

Button2 has focus Button2 有焦点

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM