Search code examples
python-3.xtkinterscreen-size

How can I get the screen width and height using appJar?


Is there a way to use appJar itself to get the screen height and width.

Alternativley since appJar is a wrapper for tkinter is there a way for me to create a Tk() instance to utilise the below code I have seen used everywhere whilst research:

import tkinter

root = tkinter.Tk()
width = root.winfo_screenwidth()
height = root.winfo_screenheight()

I would like to do this so I can use these sizes for setting window sizes later with the .setGeometry() method, e.g.

# Fullscreen
app.setGeometry(width, height)

or:

# Horizontal halfscreen
app.setGeometry(int(width / 2), height)

or:

# Vertical halfscren
app.setGeometry(width, int(height / 2))

Solution

  • Since appJar is just a wrapper over tkinter, you need a reference to root/master instance of Tk(), which stored as self.topLevel in gui. Alternatively, you can take a reference to a prettier self.appWindow, which is "child" canvas of self.topLevel.

    To make all things clear - just add some "shortcuts" to desired methods of an inherited class!

    import appJar as aJ
    
    class App(aJ.gui):
        def __init__(self, *args, **kwargs):
            aJ.gui.__init__(self, *args, **kwargs)
    
        def winfo_screenheight(self):
            #   shortcut to height
            #   alternatively return self.topLevel.winfo_screenheight() since topLevel is Tk (root) instance!
            return self.appWindow.winfo_screenheight()
    
        def winfo_screenwidth(self):
            #   shortcut to width
            #   alternatively return self.topLevel.winfo_screenwidth() since topLevel is Tk (root) instance!
            return self.appWindow.winfo_screenwidth()
    
    
    app = App('winfo')
    height, width = app.winfo_screenheight(), app.winfo_screenwidth()
    app.setGeometry(int(width / 2), int(height / 2))
    app.addLabel('winfo_height', 'height: %d' % height, 0, 0)
    app.addLabel('winfo_width', 'width: %d' % width, 1, 0)
    app.go()