1

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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

A time machine image most likely refers to the backup produced from running "Time Machine" on a mac.
@sc0tt Thanks, I'll have to check that out. Not familiar with macs.
thank you tankor, I'm having a new problem. Once the button is clicked it is invoking the function but then it stops on the first line which is top = Toplevel.tk(). The error message I receive is: File "test.py", line 95, in directory_browser top = Toplevel.tk() AttributeError: class Toplevel has no attribute 'tk. Any idea?
@bigl I can't say for sure without seeing the entire code, but it looks like you're trying to call .tk() on a TopLevel which doesn't have the tk attribute. When you want to create a new TopLevel just call it like top = TopLevel() instead.
Happy to help. Don't forget to accept the answer if it works for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.