Mark C.
Mark C.

Reputation: 1915

Dynamically resizing and adding widgets to Tkinter window

I have a Python Tkinter GUI which solicits file names from the user. I would like to add an Entry() box elsewhere in the window when each file is selected -- is it possible to do this in Tkinter?

Thanks
Mark

Upvotes: 2

Views: 4208

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385970

Yes, it is possible. You do it like you add any other widget -- call Entry(...) and then use its grid, pack or place method to have it show up visually.

Here's a contrived example:

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()

Upvotes: 5

Related Questions