簡體   English   中英

如何在下面的代碼中一個接一個地突出顯示單選按鈕

[英]how to highlight the radio buttons one after the other in following code

如何在下面的代碼中高亮顯示單選按鈕?

以下代碼處於工作狀態,但我們無法自動突出顯示按鈕(掃描):

import tkinter as tk

root = tk.Tk()
v = tk.IntVar()
v.set(1)  # initializing the choice, i.e. a

languages = [
    ("a",1),
    ("b",2),
    ("c",3),
    ("d",4),
    ("e",5)
]

def ShowChoice():
    print(v.get())

tk.Label(root, 
         text="""Choose your favourite 
programming language:""",
         justify = tk.LEFT,
         padx = 36).pack()

for val, language in enumerate(languages):
    tk.Radiobutton(root, 
                  text=language,
                  padx = 20, 
                  variable=v, 
                  command=ShowChoice,
                  value=val).pack(anchor=tk.W)


root.mainloop()

截圖:

截圖

將每個單選按鈕存儲到列表中...將其更改為

radios = []
r_config = {'padx':20, 'variable':v, 'command':ShowChoice}
for val, language in enumerate(languages):
    radios.append(tk.Radiobutton(root,  text=language,     
                  value=val,**r_config))
    radios[-1].pack(anchor=tk.W)

那么您可以稍后在更新方法中引用其配置

selected = -1
def color_tick():
    global selected # ... ewww
    radios[selected].config(bg=root.cget('bg'))
    selected = (selected+1)%len(radios)
    radios[selected].config(bg="yellow")
    # schedule a new call in 1 second
    root.after(1000,color_tick)

color_tick() # start the ticks
root.mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM