动态调整大小并将小部件添加到Tkinter窗口

问题描述:

我有一个Python Tkinter GUI,它从用户请求文件名。我想在每个文件被选中时在窗口的其他地方添加一个Entry()框 - 是否可以在Tkinter中执行此操作?动态调整大小并将小部件添加到Tkinter窗口

感谢
马克

+0

PS:我使用的是电网管理者。 –

是的,这是可能的。您可以像添加其他任何小部件那样执行此操作 - 请拨打Entry(...),然后使用其grid,packplace方法使其以可视方式显示。

这里是一个人为的例子:

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会做什么缺乏信心;这帮助我整理了一下。 –