以下是我遇到的整个程序的一部分。问题在于Tkinter Checkbuttons的用法。我正在尝试创建一个程序,该程序采用用户选择的操作列表,并根据“操作员”的数量,创建检查按钮,可以对其进行检查以识别由哪个操作员执行的操作,并通过相应检查按钮的状态进行标识州。但是不幸的是,我得到的唯一答复是我相信使用IntVar()导致的错误代码,
以下是以下代码的输出:
Tkinter.IntVar实例位于0x000000000EE332C8
所需的输出for this image本质上是下面的块引用
中显示的内容变量状态= 1,Op1,操作员1
import Tkinter
import Tkinter as tk
from Tkinter import *
class Application(tk.Frame):
def __init__(self, master=None):
self.createWidgets()
def createWidgets(self):
operator = int(6) #assigned for testing
lookingFor = ['op1','op2','op3','op4'] #assigned for testing
self.checkList = [] #creating an empty list to store Checkbox variables
cx,cy=0,0
for x in range(0, operator): ##creating left Lables
cy+=1
cx+=1
label = Label(root)
labelName = 'Operator#'
label["text"] = str(labelName)+str(cy)
label.grid(row=cx+1,column=0)
cx,cy=0,0
for y in lookingFor: ##creating top labels
cy+=1
label = Label(root)
label["text"] = str(y)
label.grid(row=cx+1,column=cy)
cx,cy=0,0
for y in range(0, operator):
cx+=1
for x in lookingFor: #creating checkboxes
var = IntVar()
c = Checkbutton(root, variable = var)
cy+=1
c.grid(row=cx+1,column=cy)
self.checkList.append(var)
cy=0
self.button = Button(root)
self.button["text"] = 'Done'
self.button["command"] = self.states
self.button.grid(row=0,column=0)
def states(self):
#This function should print out the state of each button
#as well as its corresponding 'Op#' and 'operator#'
#So the program can later decide which operations are performed
#when the program passes through each operator
print (self.checkList)
root = tk.Tk()
app = Application(master=root)
root.attributes('-topmost', True)
x=10
y=5
root.geometry("+%d+%d" % (x, y))
root.mainloop()
答案 0 :(得分:0)
首先,您得到的打印输出不是错误代码。如果直接打印IntVar
对象,则会得到该值,但是如果要打印存储在IntVar
中的值,则需要调用get()
方法。
现在,按照您的要求获取详细的输出格式,您可以为每个运算符使用类似的列表清单。
class Application(Frame):
def __init__(self, master=None):
self.createWidgets()
def createWidgets(self):
operator = int(6) #assigned for testing
lookingFor = ['op1','op2','op3','op4'] #assigned for testing
self.checkList = [[] for idx in range(operator)] #creating an empty list of lists to store Checkbox variables
cx,cy=0,0
for x in range(0, operator): ##creating left Lables
cy+=1
cx+=1
label = Label(root)
labelName = 'Operator#'
label["text"] = str(labelName)+str(cy)
label.grid(row=cx+1,column=0)
cx,cy=0,0
for y in lookingFor: ##creating top labels
cy+=1
label = Label(root)
label["text"] = str(y)
label.grid(row=cx+1,column=cy)
cx,cy=0,0
for y in range(0, operator):
cx+=1
for x in lookingFor: #creating checkboxes
var = IntVar()
c = Checkbutton(root, variable = var)
cy+=1
c.grid(row=cx+1,column=cy)
self.checkList[y].append(var)
cy=0
self.button = Button(root)
self.button["text"] = 'Done'
self.button["command"] = self.states
self.button.grid(row=0,column=0)
def states(self):
#This function should print out the state of each button
#as well as its corresponding 'Op#' and 'operator#'
#So the program can later decide which operations are performed
#when the program passes through each operator
for i, lst in enumerate(self.checkList, 1):
for j, op in enumerate(lst, 1):
print("Variable status = {}, Op{}, operator#{}".format(op.get(), j, i))
打印输出如下:
Variable status = 1, Op1, operator#1
Variable status = 0, Op2, operator#1
Variable status = 0, Op3, operator#1
Variable status = 0, Op4, operator#1
Variable status = 0, Op1, operator#2
Variable status = 1, Op2, operator#2
Variable status = 0, Op3, operator#2
Variable status = 0, Op4, operator#2
Variable status = 0, Op1, operator#3
Variable status = 0, Op2, operator#3
Variable status = 0, Op3, operator#3
Variable status = 1, Op4, operator#3
Variable status = 0, Op1, operator#4
Variable status = 0, Op2, operator#4
Variable status = 0, Op3, operator#4
Variable status = 1, Op4, operator#4
Variable status = 0, Op1, operator#5
Variable status = 0, Op2, operator#5
Variable status = 0, Op3, operator#5
Variable status = 0, Op4, operator#5
Variable status = 0, Op1, operator#6
Variable status = 1, Op2, operator#6
Variable status = 0, Op3, operator#6
Variable status = 0, Op4, operator#6