2014-12-07 82 views
1

我正在为涉及python和tkinter的计算机科学类开发一个项目。我正在努力打造一款功能齐全的大富翁游戏,并且它的进展顺利。我终于碰到了一个我似乎无法克服的障碍。我试图做一个接口来抵押用户的属性,我想用tkinter checkbuttons来获得用户输入,然后抵押所有被检查的属性。这里是类的我所做的片段:使用未知量的复选框创建窗口 - Python/tkinter

from tkinter import * 

class Mortgager(Tk): 
    def __init__(self,coorder): # 'coorder' is a class that coordinates all of the 
     self.coorder = coorder  # other classes together 

     Tk.__init__(self,className='Mortgager') 
     self.title('Mortgaging') 

     self.cbuttons = [] 
     self.intvars = [] 
     for prop in coorder.active_player.properties: # iterate through player's currently owned properties 
      if not prop.mortgaged: 
       self.intvars.append(IntVar(self,0)) 
       self.cbuttons.append(Checkbutton(self, 
               variable=self.intvars[-1],text=prop.get_name(), 
               # Most recent intvar, method returns name of property 

               command=self.update_cash_onscreen) 
               #### Not sure what to do here... 
       self.cbuttons[-1].var = self.intvars[-1] 
       self.cbuttons[-1].text = prop.get_name() 
     i = 0 
     for cbutton in self.cbuttons: # Every three properties, new column 
      cbutton.grid(column=i//3,row=i%3,sticky=W, 
         padx=5,pady=5) 
      i += 1 
     # Haven't finished the rest of the class... 

我的问题是:如何创建checkbuttons任意数量的,然后告诉它checkbuttons已被点击“在路上”更新某种Label显示当前的抵押金额,StringVar或类似的东西,然后做一些与总金额?

在此先感谢!

回答

1

我挺不理解你的代码,但如果你想在一个列表标签创建n个checkbuttons“ctrls监视”试试这个

# if ctrls is a list of all lables to your checkboxes 
# i is the count and j is the text of label 
for i,j in enumerate(ctrls): #what ever loop you want 
    var = IntVar() 
    c = Checkbutton(self.master,text=j,variable=var) 
    boxes.append([j.strip(),var,c]) 

后,如果您要检查哪些按钮被检查

for i in boxes: 
    if i[1].get()==0: 
     #do what ever you want 
     i[2].destroy()