Thread: Tkinter problem

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    24
    Rep Power
    0

    Tkinter problem


    I'm trying to get my button to stick to my frame, but I can't get it to place on the proper frame.

    Code:
    #!/usr/bin/python
    
    from Tkinter import *
    
    def run():
      print "Run button clicked"
    
    def create_buttons():
      run_button = Button(top_frame, text="Run", command=run).pack()
    
    root = Tk()
    root.title("Error Generator")
    root.geometry("500x500")
    
    top_frame = Frame(root, bg="red", height=50, width=450).pack(side=TOP)
    
    create_buttons()
    root.mainloop()
    My code is causing my button to be placed on the "root" widget. I want it on my "top_frame" frame. I thought I was packing it onto the correct frame. How do I get it on "top_frame"?
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,901
    Rep Power
    481
    top_frame is the result of the pack method. None.
    Code:
    top_frame = Frame(root, bg="red", height=50, width=450).pack(side=TOP)
    print(top_frame)
    The red rectangle surprised me. I had thought Frame objects weren't drawn.

    I'm hard pressed to think of any good reason to store run_button as a local variable in create_buttons. It will also have the value None .
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo