2017-09-25 162 views
1

有没有办法使用appJar本身来获得屏幕的高度和宽度。如何使用appJar获取屏幕宽度和高度?

因为appJar Alternativley是tkinter的包装是有我创造一个Tk()例如利用下面的代码我已经看到了到处被使用,而研究的方式:

import tkinter 

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

我想这样做,所以稍后,我可以使用这些尺寸设置窗口大小,例如,使用.setGeometry()方法

# Fullscreen 
app.setGeometry(width, height) 

或:

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

或:

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

回答

1

由于appJar仅有tkinter的包装,你需要的Tk()root/master实例,该实例存储为self.topLevelgui参考。 或者,您可以参考更漂亮的self.appWindow,这是self.topLevel的“子”画布。

为了清楚所有的事情 - 只需在继承类的所需方法中添加一些“快捷方式”即可!

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

幸运的是,appJar确实允许您创建Tk()实例。所以我能够使用函数创建一个实例来检索维度并销毁那些不需要的实例。

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