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

    Join Date
    Dec 2012
    Posts
    46
    Rep Power
    6

    Place manager not working in Tkinter


    I am trying to use the place geometry manager in Tkinter because I want to have more control over exactly how big and where my widgets get placed. I am having an issue actually getting the place() function to work with my widgets though. Here is my code:

    Code:
    from tkinter import *
    from tkinter.ttk import *
    
    class Application(Frame):
        """ GUI application for gathering recipes and creating ingredient lists. """
        def __init__(self, master):
            super(Application, self).__init__(master)
            self.pack()
            self.create_widgets()
    
        def create_widgets(self):
            # Button
            add_rcp = Button(self,
                             text = "ADD RECIPE"
                             ).place(x = 100, y = 100)
    I had it working with .grid() and .pack() but I cannot figure out how to make it work with .place().

    Any help would be greatly appreciated!
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,908
    Rep Power
    509
    Specify height and width of frame. Details later when I have proper internet connection and if I remember.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    46
    Rep Power
    6
    Originally Posted by b49P23TIvg
    Specify height and width of frame. Details later when I have proper internet connection and if I remember.
    Do I set the frame geometry in the class or in my main()? I had the window set in my main but the widgets still didn't show up. I'm using 'self.pack()' in the constructor method because you need some kind of geometry manager to activate widgets, right? I never set the frame geometry in the constructor method so maybe that's the issue?

    Ty for the reply
  6. #4
  7. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,908
    Rep Power
    509
    Code:
    import sys
    
    if sys.version[0] == '2':
        import Tkinter as tkinter
    else:
        import tkinter
    
    help(tkinter.Frame)
    
    class Application(tkinter.Frame):
    
        def __init__(self):
            super().__init__(height=300,width=600) # assign the size!
            self.pack()
            self.create_widgets()
    
        def create_widgets(self):
            tkinter.Button(self, text = 'ADD RECIPE').place(x=100,y=100)
            # the return value of place is None.  Did you really need to store that in a variable?
    
    Application().mainloop()
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    46
    Rep Power
    6
    Will you answer a couple questions?

    Why did you use tkinter. in front of Frame and Button?

    Why are there no parameters for super()?

    I saved it as a variable because I thought that was what you had to do to be able to assign an event to it.

    Thanks again!!!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    656
    Rep Power
    39
    I only use relx and rely because I can't keep track of where I am otherwise, so can only give advice with that limitation. The following code will hopefully get you started on the way.
    Code:
    class Application():
        """ GUI application for gathering recipes and creating ingredient lists.
        """
        def __init__(self, master):
            f1 = Frame(master, bd=1, relief=SUNKEN)
            f2 = Frame(master, bd=1, relief=SUNKEN)
            b1=Button(f1, text="button 1")
            b1.place(relx=0.33, rely=0, anchor="n")
            b2=Button(f1, text="button 2")
            b2.place(rely=0.5)
    
            self.create_widgets(f2)
            Button(f1, text="Quit", command=master.quit,
                   bg="orange").place(relx=0.25, rely=0.75)
            split = 0.5
            f1.place(rely=0, relheight=split, relwidth=1)
            f2.place(rely=split, relheight=1.0-split, relwidth=1)
    
            master.mainloop()
    
        def create_widgets(self, frame):
            # Button
            Button(frame, text = "ADD RECIPE\nFrame 2").place(relx=0.2,
                   rely=0.33, height=50, width=150)
    
    Application(Tk())

    Comments on this post

    • b49P23TIvg agrees : I certainly did use the relative positions and sizes in my place manager learning code.
    Last edited by dwblas; May 3rd, 2014 at 01:26 AM.
  12. #7
  13. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,908
    Rep Power
    509
    2)Having tried to understand super for several years, having found various resources such as "super is evil" and "super is super", I happened to recall that I might not need arguments to super in python3. I didn't need arguments. I didn't provide them. I have no idea why.

    1)from module import * is useful for short interpreter sessions. Programs that import * from several modules can have naming conflicts, and are hard to debug because you have to work hard to find the correct documentation.

    3)You might need
    self.add_recipe_button = tkinter.Button(args)
    self.add_recipe_button.place(args)
    tkinter.Button returns a Button object.
    The place method of a Button object returns None
    It's not the same. This error appears quite frequently in this forum. And usually it's not harmful, just an indication of mindless copying examples. Really, it's scary that so many programmers have no clue what they're doing.
    [code]Code tags[/code] are essential for python code and Makefiles!
  14. #8
  15. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    635
    Rep Power
    68
    This simply looks more clear ...
    Code:
    from tkinter import *
    from tkinter.ttk import *
    
    class Application(Frame):
        """
        GUI application for gathering recipes and creating ingredient lists.
        """
        def __init__(self, master):
            Frame.__init__(self, master, height=200, width=200)
            self.pack()
            self.create_widgets()
    
        def create_widgets(self):
            # Button
            add_rcp = Button(self, text="ADD RECIPE")
            add_rcp.place(x=100, y=100)
    
    
    root = Tk()
    app = Application(root)
    root.mainloop()
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Feb 2001
    Posts
    1,481
    Rep Power
    18
    Suppose you need to write a program to get stuff that is intelligent as well as awesome. You are not sure how to do that, so you look around at some python modules. First, you find this module:


    PHP Code:
    #mod1.py

    def get_a():
        print 
    "...getting awesome stuff"

    def done():
        print 
    "You are toast" 
    Sweet! Now you know how to get the 'awesome' part of your requirement: just import mod1 and call get_a(). That leaves you wondering how to find intelligent stuff. Looking around, you find this module:

    PHP Code:
    #mod2.py

    def get_i():
        print 
    'getting important stuff'

    def get_a():
        print 
    "getting an apple" 
    Bingo! Get on the phone and call your friends and tell them you will join them for drinks tonight. You quickly assemble your program:

    PHP Code:
    def done():
        print 
    "Woo hoo!  Let's party!"

    if "today is a Mon, Tues, Wed, Thurs, Fri, Sat, or Sun":
        
    from mod1 import *
        
    from mod2 import *

    get_i()
    get_a()
    done() 
    Let's run that baby, and I'm out the door...
    PHP Code:
    --output:--
    getting important stuff
    getting an apple
    You are toast

    Wtf?

    Lesson: you should never use "from some_module import *" in your code. Instead do something like this:

    PHP Code:
    import tkinter as tk

    root 
    tk.Tk()
    root.geometry("300x200")
    frame tk.Frame(root, ...) 

    Comments on this post

    • Dietrich agrees
    Last edited by 7stud; May 4th, 2014 at 09:16 PM.
  18. #10
  19. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    635
    Rep Power
    68
    7stud is correct, I prefer giving the import a namespace like ...
    import tkinter as tk
    to avoid import collisions, since some other module you import may have a Frame or a Button.
    Smart thing to do when you write larger programs. For example both tkinter and PIL have Image.

    Updated code ...
    Code:
    import tkinter as tk
    
    class Application(tk.Frame):
        """
        GUI application for gathering recipes and creating ingredient lists.
        """
        def __init__(self, master):
            tk.Frame.__init__(self, master, height=200, width=200)
            self.pack()
            self.create_widgets()
    
        def create_widgets(self):
            # Button
            add_rcp = tk.Button(self, text="ADD RECIPE")
            add_rcp.place(x=100, y=100)
    
    
    root = tk.Tk()
    app = Application(root)
    root.mainloop()
    Last edited by Dietrich; May 5th, 2014 at 09:06 PM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    46
    Rep Power
    6
    Awesome thank you guys. I was importing like that because the python book I am using as a reference does that. It does make sense to use a namespace to avoid module overlapping.

    Dietrich, why don't you use super() in your updated code? And why do you have to put tk in front of everything?
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    46
    Rep Power
    6
    I would also like to use ttk for the newer buttons and the extra widgets for the GUI design. How would you recommend importing that, along with tkinter?
  24. #13
  25. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    635
    Rep Power
    68
    The namespace we talked about is tk
    I avoid super() so my code works with Python2 and Python3.
    Here is an import example ...
    Code:
    try:
        # Python27
        import Tkinter as tk
        import ttk
    except ImportError:
        # Python31+
        import tkinter as tk
        import tkinter.ttk as ttk
    
    root = tk.Tk()
    
    # typical tk button
    tk_btn = tk.Button(text="tk_Sample", bg='black', fg='red')
    tk_btn.pack(pady=10)
    
    # now a ttk button with styling (bg/fg won't do)
    ttk.Style().configure("RB.TButton", foreground='red', background='black')
    ttk_btn = ttk.Button(text="ttk_Sample", style="RB.TButton")
    ttk_btn.pack(pady=10)
    
    # foreground/background works with a ttk label but not a ttk button
    # (at least not the way the way you would expect)
    style = ttk.Style()
    style.configure("GB.TLabel", foreground="green", background="blue")
    ttk_label = ttk.Label(text="ttk_Label", style="GB.TLabel")
    ttk_label.pack(pady=10)
    
    root.mainloop()
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  26. #14
  27. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    635
    Rep Power
    68
    In the previous example you deal with two namespaces tk and ttk.
    Maybe now using namespaces makes more sense to you.

    Comments on this post

    • dwblas agrees : Good point, Tk and ttk have eleven widgets that are the same
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    46
    Rep Power
    6
    Thanks dietrich. this helps a lot!

IMN logo majestic logo threadwatch logo seochat tools logo