joshuatvernon
joshuatvernon

Reputation: 1621

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))

Upvotes: 1

Views: 1118

Answers (2)

CommonSense
CommonSense

Reputation: 4482

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()

Upvotes: 2

joshuatvernon
joshuatvernon

Reputation: 1621

Fortunately, appJar does allow you to create Tk() instance. So I was able to create an instance using the functions to retrieve the dimensions and destroy the then unneeded instance.

# import appjar
from appJar import appjar

# Create an app instance to get the screen dimensions
root = appjar.Tk()

# Save the screen dimensions
width = root.winfo_screenwidth()
height = root.winfo_screenheight()

# Destroy the app instance after retrieving the screen dimensions
root.destroy()

Upvotes: 0

Related Questions