2013-11-28 3 views
1
column1 = [ 
('H', 'Hydrogen', 'AtomiC# = 1\nAtomic Weight =1.01\nState = Gas\nCategory = Alkali Metals'), 
('Li', 'Lithium', 'AtomiC# = 3\nAtomic Weight = 6.94\nState = Solid\nCategory = Alkali Metals'), 
('Na', 'Sodium', 'AtomiC# = 11\nAtomic Weight = 22.99\nState = Soild\nCategory = Alkali Metals'), 
('K', 'Potassium', 'AtomiC# = 19\nAtomic Weight = 39.10\nState = Solid\nCategory = Alkali Metals'), 
('Rb', 'Rubidium', 'AtomiC# = 37\nAtomic Weight = 85.47\nState = Solid\nCategory = Alkali Metals'), 
('Cs', 'Cesium', 'AtomiC# = 55\nAtomic Weight = 132.91\nState = Solid\nCategory = ALkali Metals'), 
('Fr', 'Francium', 'AtomiC# = 87\nAtomic Weight = 223.00\nState = Solid\nCategory = Alkali Metals')] 
#create all buttons with a loop 
r = 1 
c = 0 
for b in column1: 
    tk.Button(self,text=b[0],width=5,height=2, bg="grey",command=lambda text=b[1]:self.name(text)).grid(row=r,column=c) 
    r += 1 
    if r > 7: 
     r = 1 
     c += 1 

... : 파이썬 Tkinter를

def name(self, text): 
    self.topLabel.config(text=text) 

def info(self, text): 
    self.infoLine.config(text=text) 

나는이 튜플을 사용하고 이름에 제 2 위치 (요소 이름)을 보낼

() 함수 (현재 가지고 있고 작동하는)와 세 번째 위치 (모든 요소 정보)를 info() 함수에 전달하고 둘 다 인쇄하지만 서로 다른 위치에 있습니다. 내가 뭘하려고해도, 나는 그렇게 할 수 없을 것 같다. 튜플을 사용하여 여러 함수를 다른 함수로 보낼 수 있습니까?

당신이 당신의 버튼을 만들 줄에

답변

1

, 당신이 중 하나 (바보) 람다 트릭이 작업을 수행 할 수 있습니다

tk.Button(self,text=b[0],width=5,height=2, bg="grey", 
command=lambda text=b:[self.name(text[1]), self.info(text[2])]).grid(row=r,column=c) 

또는 둘 다 호출하는 별도의 기능 정의 :

tk.Button(self,text=b[0],width=5,height=2, bg="grey", 
command=lambda text=b:self.call_both(text)).grid(row=r,column=c) 

def call_both(self, line): 
    self.name(line[1]) 
    self.info(line[2]) 
+1

두 번째를 제안이 가장 좋습니다. 코드가하는 일이 훨씬 명확합니다. –