2011-09-04 7 views
1

일부 값을 입력 할 수있는 간단한 GUI를 만들고 싶습니다. 스크립트를 시작하기위한 앞뒤의 레이블 및 단추.tkinter 레이블과 항목을 동적으로 생성합니다.

w = Label(master, text="weight:") 
w.grid(sticky=E) 
w = Label(root, text="bodyfathydrationmuscle:bones") 
w.grid(sticky=E) 
w = Label(root, text="hydration:") 
w.grid(sticky=E) 

의 확인을하지만 난 그것을 동적 싶지 :

나는이 같은 것을 사용하고 있었다. 또한 모든 항목에 w를 사용하면 w.get을 한 번만 캐스팅 할 수 있습니다. 하지만 난 내가 생각하고 있었는데 내 모든 데이터 ;-)

필요합니다

def create_widgets(self): 
    L=["weight","bodyfat","hydration","muscle","bones"] 
    LV=[] 
    for index in range(len(L)): 
     print(index) 
     print(L[index]) 
     ("Entry"+L[index])= Entry(root) 
     ("Entry"+L[index]).grid(sticky=E) 
     ("Label"+L[index])=Label(root, text=L[index]) 
     ("Label"+L[index]).grid(row=index, column=1) 

나중에 호출하려면 : 등등

var_weight=Entryweight.get() 
var_bodyfat=Entrybodyfat.get() 

하고 있습니다. 어떻게 작동시킬 수 있습니까?

답변

6

귀하의 프로그램은 Entrybodyfat이고 다른 변수는 generated on the fly이어야하지만 그렇게하고 싶지는 않습니다.

일반적인 방법은 목록이나지도의 항목 및 라벨을 저장하는 것입니다 다음 체지방 항목의

from Tkinter import * 

root = Tk() 

names = ["weight", "bodyfat", "hydration", "muscle", "bones"] 
entry = {} 
label = {} 

i = 0 
for name in names: 
    e = Entry(root) 
    e.grid(sticky=E) 
    entry[name] = e 

    lb = Label(root, text=name) 
    lb.grid(row=i, column=1) 
    label[name] = lb 
    i += 1 

def print_all_entries(): 
    for name in names: 
     print entry[name].get() 

b = Button(root, text="Print all", command=print_all_entries) 
b.grid(sticky=S) 

mainloop() 

값은 다음 entry["bodyfat"].get()입니다.

관련 문제