2011-09-07 9 views
2

나는 사용자로부터 파일 이름을 요구하는 Python Tkinter GUI를 가지고 있습니다. 각 파일을 선택하면 윈도우의 다른 곳에 엔트리() 상자를 추가하고 싶습니다. Tkinter에서이 작업을 수행 할 수 있습니까?동적으로 크기를 조정하고 Tkinter 창에 위젯을 추가합니다.

감사
마크

+0

PS :

는 여기에 인위적인 예제 나는 그리드 관리자를 사용하고 있습니다. –

답변

4

네, 가능합니다. 다른 위젯을 추가하는 것처럼 할 수 있습니다. Entry(...)으로 전화 한 다음 grid, pack 또는 place 메서드를 사용하여 시각적으로 표시 할 수 있습니다.

import Tkinter as tk 
import tkFileDialog 

class SampleApp(tk.Tk): 
    def __init__(self, *args, **kwargs): 
     tk.Tk.__init__(self, *args, **kwargs) 
     self.button = tk.Button(text="Pick a file!", command=self.pick_file) 
     self.button.pack() 
     self.entry_frame = tk.Frame(self) 
     self.entry_frame.pack(side="top", fill="both", expand=True) 
     self.entry_frame.grid_columnconfigure(0, weight=1) 

    def pick_file(self): 
     file = tkFileDialog.askopenfile(title="pick a file!") 
     if file is not None: 
      entry = tk.Entry(self) 
      entry.insert(0, file.name) 
      entry.grid(in_=self.entry_frame, sticky="ew") 
      self.button.configure(text="Pick another file!") 

app = SampleApp() 
app.mainloop() 
+0

고마워요! 내 문제는 부분 초보자 오류 였고 Tk가 할 일에 대한 확신이 없었습니다. 이것은 내가 그것을 소트하는 것을 도왔다. –

관련 문제