Python Forum
Tkinter positional information issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tkinter positional information issue
#1
Hi again, I am having issues getting the Tkinter window position. Here is the code example (very bare bones with print statements

import tkinter as tk
import re as regexp

class test_window(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self.parent = parent
        self.parent.geometry("400x500")
        self.center_points()
    def center_points(self):
        #We have the height and width of the window, we know we need 10 Hexes vertical and 8 hexes horizontal. We need to get a size for the hexes, then we offset the first hex in the xy direction by 1/2 the respective length to position it properly
        
        self.geo = regexp.findall('\d+',self.parent.geometry())
        print(self.geo)
        print(self.parent.geometry())
        print(self.parent.winfo_reqheight())
        print(self.parent.winfo_reqwidth())
        

if __name__ == "__main__":
    root = tk.Tk()
    main = test_window(root)
    root.mainloop()
For the output I am getting:

['1', '1', '0', '0']
1x1+0+0
200
200

I set the window to be 400x500... This is not the first time a widget that I have tried to get positional information out of has given me wrong information. Could someone tell me if they can reproduce this result? I am using Tkinter version 8.6 with Spyder.
Reply
#2
use: widget_name.cget("attribute")
Reply
#3
Because of the position information was printed before mainloop is running, the value you get is initially "1x1+0+0'. You have to do self.update_idletasks() before using geometry() to get the correct value.

self.update_idletasks()
self.geo = .....
Reply
#4
or so:
        self.parent.geometry("400x500")
        self.parent.update_idletasks()
wuf ;-)
Reply
#5
So to prove the theory, try a button and see if clicking (i.e. after mainloop) will provide the measurements

import tkinter as tk
import re as regexp

class test_window():
    def __init__(self, parent, *args, **kwargs):
        self.parent = parent
        self.parent.geometry("400x500")
##        self.center_points()
        tk.Button(self.parent, text="Get dimensions",
                  command=self.center_points).grid()

    def center_points(self):
        #We have the height and width of the window, we know we need 10 Hexes vertical and 8 he
##        self.parent.update_idletasks()
##        print(self.geo)
        print(self.parent.geometry())
        print(self.parent.winfo_reqheight())
        print(self.parent.winfo_reqwidth())


if __name__ == "__main__":
    root = tk.Tk()
    main = test_window(root)
    root.mainloop() 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Lightbulb [Tkinter] Tkinter Class Import Module Issue AaronCatolico1 6 3,096 Sep-06-2022, 03:37 PM
Last Post: AaronCatolico1
  Super basic tkinter arduino issue Kurta 3 2,413 Jan-07-2021, 05:22 PM
Last Post: deanhystad
Photo tkinter issue mate 4 2,530 Dec-06-2020, 09:03 PM
Last Post: mate
  Issue in Tkinter with winfo_class() and LabelFrame ReDefendeur 1 2,737 Oct-05-2020, 05:52 AM
Last Post: Jeff900
  [Tkinter] tkinter: after issue edwin6938 1 3,390 Aug-25-2020, 04:37 PM
Last Post: Larz60+
  Tkinter: increasing numbers and Radiobutton issue PeroPuri 1 2,158 Apr-13-2020, 05:48 PM
Last Post: deanhystad
  [Tkinter] tkinter issue with variables carrying over between functions PengEng 1 1,732 Apr-06-2020, 06:13 PM
Last Post: deanhystad
  Issue on tkinter with buttons Reldaing 1 2,440 Jan-07-2020, 08:21 AM
Last Post: berckut72
  [Tkinter] Tkinter window issue frequency 4 3,347 Dec-24-2018, 10:49 AM
Last Post: frequency

Forum Jump:

User Panel Messages

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