bigl
bigl

Reputation: 1083

tkinter python create child window

Hi I am trying to create a tool that browses a time machine image with Tkinter in python. I plan on using the code from here: http://code.google.com/p/python-ttk/source/browse/trunk/pyttk-samples/dirbrowser.py?r=21 for the directory browser. I have written a start menu and upon clicking on the 'browse' button I want the directory browser to open where the user can select a file and the path is then passed back to the Label (I need to add this as its not in the directory browser code yet). Below is the code for my start menu:

#!/usr/bin/python

from Tkinter import *
import ttk

class App:

    def __init__(self,master):

        frame = Frame(master)
        frame.pack()

        self.label = Label(frame, text="Please enter file path or browse to a file")
        self.label.pack(side=TOP)

        self.button = Button(frame, text="OK", command=messageWindow)
        self.button.pack(side=BOTTOM)

        self.hi_there = Button(frame, text="Browse")
        self.hi_there.pack(side=BOTTOM)

        self.entry = Entry(frame, width = 30)
        self.entry.pack(side=LEFT)

root = Tk()

app = App(root)

root.mainloop()

I have read that you cannot have two root frames at once with Tkinter but I am struggling to find an alternative as the directory browser also has a root frame. I am not sure if what I am doing is correct but on the button for browse I have added:

 self.hi_there = Button(frame, text="Browse", command=dir)

I have put the directory browser code inside of a class and called it dir. So my thinking is that I should be calling the entire class? But then I get an error stating that the name dir is not defined. What ways can I go about getting this right?

Upvotes: 1

Views: 1331

Answers (1)

TankorSmash
TankorSmash

Reputation: 12747

I don't quite understand what you mean by "a time machine image", but I've got a few things that might help you: don't use the variable name dir, as that's a builtin keyword and you're bound to run into problems. If you're having trouble finding the method called dir which is inside a class, make sure you're telling it to look inside the class.

    def sayHello():
        print "Hello!"


    class Person:

        def sayHello():
            print "Hello from Person"

    a_person = Person()

    sayHello() 
    ##"Hello"

    a_person.sayHello()
    ## "Hello from Person"

Calling printHello and class_instance.printHello are two different functions, and you'll want to pass class_instance.dir to the button.

I'm sure you know about them, but there are premade file dialogs to help with getting filepaths, filenames etc.

Another thing is you don't want a new root instance, you're looking for a new TopLevel instance, which is essentially the same thing as a new root, but not quite.

Upvotes: 1

Related Questions