2016-08-25 3 views
1

는 지금은이 :tk.Frames를 만들고 구성하는 반복적 인 방법이 있습니까?

# create window frames 
    self.f1 = tk.Frame(self.root) 
    self.f2 = tk.Frame(self.root) 
    self.f3 = tk.Frame(self.root) 
    self.f4 = tk.Frame(self.root) 
    self.f5 = tk.Frame(self.root) 
    self.f6 = tk.Frame(self.root) 

# place frames on window 
    for f in (self.f1, self.f2, self.f3, self.f4, self.f5, self.f6): 
     f.configure(bg="white") 
     f.configure(width=self.width, height=self.height, bg="white") 
     f.place(x=0, y=0) 

내가 훨씬 더 많은 프레임을 추가 할 예정입니다. "self.f7, self.f8, self.f9"를 입력 할 필요없이 모든 프레임을 만들고 창에 배치하고 구성 할 수있는 반복적 인 방법이 있는지 궁금합니다.

답변

2

모든 새 Frame을 목록에 추가 한 다음 목록을 반복합니다.

frames = [] 

self.f1 = tk.Frame(self.root) 
frames.append(self.f1) 
# Do that for all frames 

for f in frames: 
    f.configure(bg="white") 
    f.configure(width=self.width, height=self.height, bg="white") 
    f.place(x=0, y=0) 

편집 코멘트에 대답 :

는 방법을 만들기 :

def add_frames(self, how_many_frames): 
    for i in range(how_many_frames): 
     f = tk.Frame(self.root) 
     self.frames[i] = f 
     f.configure(bg="white") 
     f.configure(width=self.width, height=self.height, bg="white") 
     f.place(x=0, y=0) 

는 또한 self.frames = dict()__init__ 방법에 초기화가 필요합니다. 이제 add_frames(30)을 호출하여 30 개의 프레임을 만들고 self.frames에 사전에 저장하고 동시에 구성하십시오.

+0

하지만 추가하기 전에 각 "프레임"개체를 선언해야 할 필요가 없을까요? 정수를 입력 할 수있는 방법이 있습니까 (30 번). 자동으로 목록에 채워지게합니다. self.f1, self.f2, self.f3? – RBuntu

+0

이게 당신이 찾고 있는게 있나요? – grael

+0

거의! 국제 대회 f1, f2, f3 등으로 어떻게 이름을 지어야합니까? 그 이름을 사전에 키로 저장합니까? (즉, 어떻게 개별적으로 액세스 할 수 있습니까? "self.frames [1]", "self.frames [2]", "self.frames [3]"등을 사용해야합니까? – RBuntu