Python Forum
New at Python programming
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
New at Python programming
#1
I'm a VB.Net programmer by trade and am in process of learning Python. I'm messing around with the tkinter library for GUI development. One simple thing that I'm trying to figure out...and can't, is how to make a Window a specific size? Can someone suggest a good site for tkinter tutorials?

Thanks in advance
Reply
#2
after you create the main window:
import tkinter as tk

root = tk.Tk()
# The set geometry
root.geometry('600x400+10+10')
root.mainloop()
the string is 'widthxheight+xoffset+yoffset'

if you are running on windows, the following class will get you screen sizes of all monitors
attached to your computer, and calculate window size based on percentage of full screen:
import sys
import screeninfo
import re


"""
GetScreenInfo()

cross platform creates a dictionary with an entry for each monitor
tied to users computer. the key is constructed as follows:
   monitor{n} n is sequentiallty assigned beginning with 1

Usage: GetScreenInfo(win_scale=.6, offset_scale=.1)
   where: scale is % of full screen, so default .6 = 60 % and
          offset is % of scaled width & height

each entry contains a nested dictionary with four values:
   swidth = scaled_width
   sheight = scaled height
   hoffset = horizontal offset
   voffset = vertical offset4

example:
>>> from GetScreenInfo import GetScreenInfo
>>> gsi = GetScreenInfo(win_scale=.8, offset_scale=.2)
>>> print(gsi.monitor_info)
>>>
    {'monitor1': {'swidth': 1920, 'sheight': 1080, 'hoffset': 384, 'voffset': 216}}

Needs testing on Apple OS-X

Author: Larz60+
"""
class GetScreenInfo:
    def __init__(self, win_scale=.6, offset_scale=.1):
        # use scale of 0 to return unmodified dimensions
        if win_scale == 0:
            newscale = 1
            newoffset = 0
        else:
            newscale = win_scale
            newoffset = offset_scale

        platform = sys.platform
        platform = platform.rstrip('1234567890')
        self.monitor_info = {}
        ostypes = {
            'linux': 'x11',
            'win': 'windows',
            'cygwin': 'cygwin',
            'darwin': 'osx'
        }

        # Following hack is for return proper linux value from sys.platform
        #  prior to python 3.3 which always starts with 'linux' but may
        #  be linux1, linux2 etc.
        if platform.startswith('linux'):
            montype = ostypes['linux']
        mon = screeninfo.get_monitors(ostypes[platform])
        # print(f'mon: {mon}')
        for n, item in enumerate(mon):
            m = re.split(r'[()x+]', str(item))
            mkey = 'monitor{}'.format(n + 1)
            self.monitor_info[mkey] = {}
            self.monitor_info[mkey]['swidth'] = int(float(m[1]) * newscale)
            self.monitor_info[mkey]['sheight'] = int(float(m[2]) * newscale)
            self.monitor_info[mkey]['hoffset'] = int(float(m[1]) * newoffset)
            self.monitor_info[mkey]['voffset'] = int(float(m[2]) * newoffset)

def main():
    gsi = GetScreenInfo(win_scale=.4, offset_scale=.2)
    print(gsi.monitor_info)

if __name__ == '__main__':
    main()
you will need the screeninfo package. you can get this with (from command line):
pip install screeninfo
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyGUI] Start learning GUI Programming python sumandas89 1 3,179 Jan-18-2019, 02:11 PM
Last Post: Larz60+
  [split] New at Python programming Akash 1 2,784 Nov-10-2017, 07:30 PM
Last Post: heiner55
  [PyGUI] Opinions on Python GUI programming cdn 2 4,901 Aug-26-2017, 11:56 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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