【问题标题】:How can I get the screen width and height using appJar?如何使用 appJar 获取屏幕宽度和高度?
【发布时间】:2017-09-25 12:58:41
【问题描述】:

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

Alternativley 因为appJartkinter 的包装器,有没有办法让我创建一个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))

【问题讨论】:

    标签: python-3.x tkinter screen-size


    【解决方案1】:

    由于appJar 只是tkinter 的包装,您需要引用root/masterTk() 实例,它在gui 中存储为self.topLevel。 或者,您可以参考更漂亮的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()
    

    【讨论】:

      【解决方案2】:

      幸运的是,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()
      

      【讨论】:

        猜你喜欢
        • 2011-09-25
        • 1970-01-01
        • 2010-11-25
        • 2011-08-06
        • 1970-01-01
        • 2012-11-07
        • 2011-06-12
        • 1970-01-01
        相关资源
        最近更新 更多