tkinter单击后无法更改单选按钮的外观

时间:2019-06-19 04:47:26

标签: python tkinter

我已经阅读了许多示例,并且我的代码看起来很相似,但是当我单击单选按钮时,它并没有像预期的那样变成绿色。

代码需要包含以下行的csv文件

ref,Lang1,Lang2,Lang3,Lang4

我知道代码是正确执行的,通过在整个代码中使用调试打印语句-已删除许多代码以简化代码

import tkinter as tk
import csv as csv

root = tk.Tk()
Sel_Lang = tk.StringVar()

def radioselect():
    global lasthit
    temp = int(Sel_Lang.get()) -1
    buttonlist[temp].config(bg='green')
    buttonlist[temp].grid(row=temp, column=1)
    if lasthit != temp:
        print('last hit greater then 0')
        buttonlist[lasthit].config(bg='white')
        buttonlist[lasthit].grid(row=lasthit, column=1)
    lasthit = temp

with open('Language.csv') as csvfile:
    Langptr = csv.reader(csvfile, delimiter=',')
    row1 = next(Langptr)   #read the header row

langs = (len(row1))
lang=1

while lang < langs:
    ##print(row1[lang])
    MODES.append((row1[lang], lang))
    lang = lang + 1

MODES=[]
lasthit = 0
arraycntr = 0
buttonlist = [0] * len(MODES)
for text, mode in MODES:
    '''  display for the user to select Language
    language choices are taken from the first row in the Language.csv file
    '''
    buttonlist[arraycntr] = tk.Radiobutton(root, height=2, width=15, 
        borderwidth=10, text=text, font=("Arial", 24, "bold"), bg='white',
        variable=Sel_Lang, value=mode, indicatoron=0)
    buttonlist[arraycntr].config(command = lambda :radioselect())
    buttonlist[arraycntr].grid(row=mode, column=1)
    print('In for loop ', arraycntr, text, mode, 
        len(MODES),buttonlist[arraycntr])
    arraycntr += 1

root.mainloop()

没有错误消息,但是按下的按钮没有按预期的那样变成绿色

1 个答案:

答案 0 :(得分:0)

您需要添加选项selectcolor

buttonlist[arraycntr] = tk.Radiobutton(root, height=2, width=15, 
  borderwidth=10, text=text, font=("Arial", 24, "bold"), bg='white',
  variable=Sel_Lang, value=mode, indicatoron=0, selectcolor='green')