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

Mar*_* C. 2 python user-interface tkinter

我有一个 Python Tkinter GUI,它从用户那里请求文件名。我想在选择每个文件时在窗口中的其他位置添加一个条目()框 - 是否可以在tkinter中执行此操作?

感谢
马克

Bry*_*ley 5

对的,这是可能的。您这样做就像添加任何其他小部件一样 - 调用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()
Run Code Online (Sandbox Code Playgroud)