1

What is the correct syntax for accessing the 'x' and 'y' values in the 'place' attribute of a TKinter Checkbutton? I'm not finding anything to help in the docs and my attempts to guess the syntax haven't worked.

from tkinter import *
window=Tk()
v1 = IntVar()
arCombo = []

C1 = Checkbutton(window, state = "normal", text = "Ctrl", variable = v1)
C1.place(x=275, y=65)

arCombo.append(C1)

# these two successfully retrieve 'text' and 'state':
print(arCombo[0].cget("text"))
print(arCombo[0].cget("state"))

# How to access 'x' & 'y' values in 'place'?
# None of these work:
print(arCombo[0].cget("x"))
print(arCombo[0].cget("place"))
print(arCombo[0].place.cget("x"))
print(arCombo[0].place("x"))

window.title('Title')
window.geometry("800x600+10+10")
window.mainloop()
hermitjimx
  • 61
  • 10

1 Answers1

1

You can call the method place_info on a widget that was laid out using place. When called, it will return a dictionary that contains all of the information about all of the values used to place the window.

In your case, the dictionary looks like this:

{   
    'anchor': 'nw',
    'bordermode': 'inside',
    'height': '',
    'in': <tkinter.Tk object .>,
    'relheight': '',
    'relwidth': '',
    'relx': '0',
    'rely': '0',
    'width': '',
    'x': '275',
    'y': '65'
}

You can then query the value in the dictionary as you would with any dictionary. For example, you can print out the x coordinate like so:

print(arCombo[0].place_info()['x'])

Note: the values in the dictionary are all strings. If you want to treat the values as integers you must explicitly convert them to integers.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685