Python Forum
Using place for a button, many examples but AttributeError, tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using place for a button, many examples but AttributeError, tkinter
#1
This must be very simple, how to Place a button by x and y coordinates, not using a frame/grid.

button.place(x=100, y=200)
Shown in examples not working (for me).

My test code:
from tkinter import *
from tkinter import messagebox, ttk

root = Tk() 
root.title("  Place button  ")
root.geometry( "300x300" ) #(HxV)

label = Label( root , text = " " ) 
label.pack()

label1 = Label( root , text = "Set Value " ) 
label1.place(x=35, y=140)

combo = ttk.Combobox(values=["300","250","225","200","175","150","140","130","125"])
combo.bind("<<ComboboxSelected>>")
#combo.pack(padx=10,pady=10)
combo.place(x=100, y=140)

button = Button( root , text = "Save changes").pack() 
#button.place(x=100, y=200)  #AttributeError: 'NoneType' object has no attribute 'place'

root.mainloop()
I found this:
Quote:Don't mix layout managers (pack, grid, place) on the same parent widget. If you use pack or grid on a widget that contains the button, place might not work as expected.
Really vague, so place works with some widgets and not others...
Reply
#2
from tkinter import *
from tkinter import messagebox, ttk

root = Tk()
root.title("  Place button  ")
root.geometry( "300x300" ) #(HxV)

label = Label( root , text = " " )
label.pack()

label1 = Label( root , text = "Set Value " )
label1.place(x=35, y=140)

combo = ttk.Combobox(values=["300","250","225","200","175","150","140","130","125"])
combo.bind("<<ComboboxSelected>>")
#combo.pack(padx=10,pady=10)
combo.place(x=100, y=140)

button = Button( root , text = "Save changes")
button.place(x=100, y=200)  #AttributeError: 'NoneType' object has no attribute 'place'

root.mainloop()
Botton(...) returns a Button instance and the method pack() on this instance returns None, not the Button. You're assigning None to button. Also, you can use only one system to place the objects: pack, grid and place. They are mutually exclusive.

Additionally, star imports are bad.
Improved version without star imports and used a code formatter (ruff format).
from tkinter import Tk, Label, Button
from tkinter import ttk

root = Tk()
root.title("  Place button  ")
root.geometry("300x300")  # (HxV)

label = Label(root, text=" ")
label.pack()

label1 = Label(root, text="Set Value ")
label1.place(x=35, y=140)

combo = ttk.Combobox(
    values=["300", "250", "225", "200", "175", "150", "140", "130", "125"]
)
combo.bind("<<ComboboxSelected>>")
combo.place(x=100, y=140)

button = Button(root, text="Save changes")
button.place(x=100, y=200)

root.mainloop()
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
Thanks DE

Oh yes, the .pack() at the end of the button line caused this.
will loose the *
Reply
#4
What is the purpose of bind?
combo = ttk.Combobox(values=["300","250","225","200","175","150","140","130","125"])
combo.bind("<<ComboboxSelected>>")
This makes sense because it binds a function to be called when a combobox item is seleted.
combo = ttk.Combobox(values=["300","250","225","200","175","150","140","130","125"])
combo.bind("<<ComboboxSelected>>", some_function)
I avoid using place(). I use grid() or pack() instead. Using a geometry manager is much quicker (once you learn how to use them) and you don't have to worry about things like changing font size messing up your layout. Eventually you will want to support styling, and that pretty much eliminates the option of using place() to position the widgets.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  tkinter Radio button group not setting default selection simonc88 3 848 Mar-20-2025, 06:53 PM
Last Post: buran
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 4,613 Jan-16-2024, 06:44 PM
Last Post: rob101
  tkinter - touchscreen, push the button like click the mouse John64 5 2,461 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Place QT Window in the middle AlphaInc 10 6,200 Aug-03-2023, 05:40 PM
Last Post: Axel_Erfurt
  Centering and adding a push button to a grid window, TKinter Edward_ 15 14,141 May-25-2023, 07:37 PM
Last Post: deanhystad
  tkinter AttributeError: 'GUI' object has no attribute pfdjhfuys 3 4,158 May-18-2023, 03:30 PM
Last Post: pfdjhfuys
  [Tkinter] how to make label or button not visible with the place method? nowayj63 2 5,216 Jan-03-2023, 06:29 PM
Last Post: Yoriz
  Can't get tkinter button to change color based on changes in data dford 4 4,820 Feb-13-2022, 01:57 PM
Last Post: dford
  Creating a function interrupt button tkinter AnotherSam 2 8,108 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 7,486 Oct-01-2021, 05:00 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020