0

I have managed to have an opening page, followed by a second page which contains a file browser button. I would like to have the option for user entry to this file browser (copy and paste into an entry bar). Following either the directory being user entered, or browsed using the button, I would like the directory to appear in the bar.

import tkinter as tk
from tkinter import ttk
from tkinter.filedialog   import askdirectory  

LARGE_FONT= ("Calibri", 12)

class RevitJournalSearch(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in (StartPage, PageOne):               
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(StartPage)

    def show_frame(self, cont):
        frame = self.frames[cont]
        frame.tkraise()

class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        label = ttk.Label(self, text="""What to expect from this...
                          \n
                          \n-Browse folder location""", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button1 = ttk.Button(self, text="Continue",
                            command=lambda: controller.show_frame(PageOne))
        button1.pack()
        button2 = ttk.Button(self, text="Close",
                            command=quit)
        button2.pack()

class PageOne(tk.Frame):

    def __init__(self, parent, controller, master):
        tk.Frame.__init__(self,parent)
        label = ttk.Label(self, text="File Select", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        errmsg = 'Error!'
        button_browser = ttk.Button(self, text="BROWSE",
                                    command= self.callback)
        button_browser.pack()

    def callback(self):
        self.name= askdirectory()

app = RevitJournalSearch()
app.mainloop()

I'm not sure where to place the self.entry code.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tfarks
  • 1
  • 1
  • 2

1 Answers1

1

I believe this is a duplicate of this question. What it boils down to is the following.

  1. Create a StringVar variable, and then create an entry box that is dependent on this StringVar. The box will display whatever this variable is currently holding.

    self.name = tk.StringVar()
    dir_box = tk.Entry(self, textvariable=self.name)
    
  2. Set this variable with the return value of your askdirectory call.

    def callback(self):
        filename = tkFileDialog.askdirectory()
        self.name.set(filename)
    
Community
  • 1
  • 1
Hobbes
  • 404
  • 3
  • 9