Python Forum
[Tkinter] Window geometry appears different on Win and Linux
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Window geometry appears different on Win and Linux
#1
I write my code in Windows 7 using IDLE on Python v3.67.

When I try out Tkinter code on Linux Mint 19.1, also using Idle and Python 3.67,
the GUI window of my programs always come out too small,
typically not wide enough and not long enough.

The screen resolution I use on both OS's are the same.
Is this problem normal?

I fix the problem by checking the platform and each having their own window geometry,
but it just doesn't feel right.

Example:

if sys.platform.startswith('win'):
    root.geometry('464x632')
else:
    root.geometry('517x663')
Reply
#2
Here's a class that will get the geometry for all monitors on your computer
 
# Author: Larz60+ Nov 2019

from screeninfo import get_monitors


class GetGeometry:
    def __init__(self):
        self.geometry = []

    def get_tkinter_geometry(self, percent_of_screen, xpad=None, ypad=None):
        '''
        Given percent of monitor size in floating point eg: 10 % = 10.0, calculates
        tkinter geometry for each monitor attached to computer
        requires screeninfo "pip install screeninfo"

        returns: list holding tkinter geometry strings padded with xpad and ypad
                or centered if xpad is None.
                
                None if bad pct passed
        '''

        if not isinstance(percent_of_screen, float):
            print("requires float percent eg: 10.0 for 10%")
            return

        pct = percent_of_screen / 100

        for size in get_monitors():
            cwidth = int(size.width * pct)
            cheight = int(size.height * pct)

            xoff = xpad
            yoff = ypad
            if xpad is None:
                xoff = int((size.width - cwidth) / 2)
                yoff = int((size.height - cheight) / 2)
            self.geometry.append(f"{cwidth}x{cheight}+{xoff}+{yoff}")
test function:
import GetTkGeometry


gg = GetTkGeometry.GetTkGeometry()
# padding specified 10 % of screen dimensions
gg.get_tkinter_geometry(10.0, 10, 10)
geometry = gg.geometry
print(f"\ngeometry - 10% of monitor size 10 pixel x and y padding:\n{geometry}")

# padding not specified 60 % of screen dimensions
gg.get_tkinter_geometry(60.0)
geometry = gg.geometry
print(f"\ngeometry - 60% of monitor size centered (no pading specified):\n{geometry}")
results for my system:
Output:
geometry - 10% of monitor size 10 pixel x and y padding: ['192x108+10+10'] geometry - 60% of monitor size centered (no pading specified): ['192x108+10+10', '1152x648+384+216']
Reply
#3
That's a clever piece of code Larz, thank you,
but my question was:

The screen resolution I use on both OS's are the same.
yet the geometries come out differently on Windows and Linux.
Is this problem normal?
Reply
#4
that shouldn't be the case, although there are some physical differences that might make the size appear to be different.
I don't have a windows computer, so cannot test.
is the geometry statement identical on both OS's?
Reply
#5
(Nov-25-2019, 11:38 PM)Larz60+ Wrote: that shouldn't be the case, although there are some physical differences that might make the size appear to be different.
I don't have a windows computer, so cannot test.
is the geometry statement identical on both OS's?

The first root.geometry below works on Windows.
When that geom is run on my Linux Mint setup it comes out different,
not wide enough and too short.
So I check if not Windows and the 2nd geom
is the adjustment Linux needs to make it look the same as on Windows.

53 extra pixels wide and 31 extra pixel high.

if sys.platform.startswith('win'):
    root.geometry('464x632')
else:
    root.geometry('517x663')
Reply
#6
The class that I provided will set the offset as well.
Also, your code is telling tkinter to use a different geometry if not windows, why?
(your if platform ...)
Reply
#7
It's weird. If I do not set the root.geometry and leave it to the system
the GUI comes out fine on both Windows and Linux so I have a way around it.
Thanks for trying to help, I don't think I explained the problem clearly
and I certainly didn't understand your replies, sorry about that, but think sorted now
anyway. cheers.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Interaction between Matplotlib window, Python prompt and TKinter window NorbertMoussy 3 343 Mar-17-2024, 09:37 AM
Last Post: deanhystad
  Tkinter multiple windows in the same window tomro91 1 787 Oct-30-2023, 02:59 PM
Last Post: Larz60+
  Beginner question re: Tkinter geometry return2sender 3 905 Jun-19-2023, 06:19 PM
Last Post: deanhystad
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,380 May-25-2023, 07:37 PM
Last Post: deanhystad
  [Tkinter] Open tkinter colorchooser at toplevel (so I can select/focus on either window) tabreturn 4 1,831 Jul-06-2022, 01:03 PM
Last Post: deanhystad
  [Tkinter] Background inactivity timer when tkinter window is not active DBox 4 2,866 Apr-16-2022, 04:04 PM
Last Post: DBox
  why my list changes to a string as I move to another window in tkinter? pymn 4 2,546 Feb-17-2022, 07:02 AM
Last Post: pymn
  [Tkinter] Tkinter Window Has no Title Bar gw1500se 4 2,795 Nov-07-2021, 05:14 PM
Last Post: gw1500se
  "tkinter.TclError: NULL main window" Rama02 1 5,782 Feb-04-2021, 06:45 PM
Last Post: deanhystad
Question [Tkinter] How to change geometry? Meffy 4 2,839 Jan-31-2021, 10:35 AM
Last Post: Meffy

Forum Jump:

User Panel Messages

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