python中的ttk通过类改变按钮的状态,提示keyerror?
想实现点击按钮后,让它变成不可点击状态。小白提勿喷。。importtkinterastkfromtkinterimportttkui=tk.Tk()classb_choo...
想实现点击按钮后,让它变成不可点击状态。小白提勿喷。。
import tkinter as tk
from tkinter import ttk
ui=tk.Tk()
class b_choose:
def __init__(self,i):
self.i=i
def click(self):
print("第",self.i,"个按钮")
name = 'b_'+str(i)
# 就是这里vars()[name]错误keyerror,怎么改?
vars()[name].configure(state='disabled')
#创建9个按钮
b_row=0
b_column=0
for i in range(0,9):
if b_column==3:
b_row +=1
b_column=0
name="b_"+str(i)#设置按钮名,如B_1,B_2到B_9并定义到类
vars()[name]=b_choose(i)
vars()[name]=ttk.Button(ui,text="无",command=vars()[name].click)
vars()[name].configure(width=10,padding=20)
vars()[name].grid( row=b_row,column=b_column, sticky='W')
b_column+=1
ui.mainloop() 展开
import tkinter as tk
from tkinter import ttk
ui=tk.Tk()
class b_choose:
def __init__(self,i):
self.i=i
def click(self):
print("第",self.i,"个按钮")
name = 'b_'+str(i)
# 就是这里vars()[name]错误keyerror,怎么改?
vars()[name].configure(state='disabled')
#创建9个按钮
b_row=0
b_column=0
for i in range(0,9):
if b_column==3:
b_row +=1
b_column=0
name="b_"+str(i)#设置按钮名,如B_1,B_2到B_9并定义到类
vars()[name]=b_choose(i)
vars()[name]=ttk.Button(ui,text="无",command=vars()[name].click)
vars()[name].configure(width=10,padding=20)
vars()[name].grid( row=b_row,column=b_column, sticky='W')
b_column+=1
ui.mainloop() 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询