如何将我的Kivy应用程序窗口定位在右下角

huangapple go评论74阅读模式
英文:

How can I position my kivy app window in the right bottom corner

问题

我有一个Kivy桌面应用程序,想要将其启动在右上角。
目前,我只能应用左上角的位置...

from kivy.config import Config
Config.set('graphics', 'position', 'custom')
Config.set('graphics', 'resizable', 0)
Config.set('graphics', 'left', 20)
Config.set('graphics', 'top', 50)
Config.write()
英文:

I have a kivy desktop app and want to start it in the corner at the right.
Currently, i can only apply the position from the left, top...

from kivy.config import Config
Config.set('graphics','position','custom')
Config.set('graphics','resizable',0)
Config.set('graphics','left',20)
Config.set('graphics','top',50)
Config.write()

答案1

得分: 1

我相信使用 lefttop 属性是定位应用程序的唯一方法。如果这是真的,那么你必须获取设备屏幕的大小,并使用它来计算 topleft。然而,在Python/Kivy中获取设备屏幕的大小是困难的。你可以通过将Kivy窗口最大化并使用其大小来获取一个近似值。以下是遵循这种方法的一些代码:

def do_maximize(self, dt):
    self.app_size = Window.size
    Window.maximize()
    Clock.schedule_once(self.adjust_window)

def adjust_window(self, dt):
    window_size = Window.size
    top = Window.top
    left = Window.left
    Window.restore()
    Window.top = window_size[1] - self.app_size[1] + top
    Window.left = window_size[0] - self.app_size[0] + left

并且在 App.build() 方法中调用:

Clock.schedule_once(self.do_maximize)

一个更精确但更复杂的方法是使用 subprocess 运行一个命令来捕获设备的大小(例如在Linux上使用 xdpyinfo),然后解析输出来获取大小。

英文:

I believe that using the left and top properties are the only way to position an app. If that is true, then you must get the size of the device screen, and use that to calculate the the top and left. However, getting the size of the device screen is difficult in python/kivy. You can get an approximation by maximizing the kivy window and using its size. Here is some code that follows this approach:

def do_maximize(self, dt):
    self.app_size = Window.size
    Window.maximize()
    Clock.schedule_once(self.adjust_window)

def adjust_window(self, dt):
    window_size = Window.size
    top = Window.top
    left = Window.left
    Window.restore()
    Window.top = window_size[1] - self.app_size[1] + top
    Window.left = window_size[0] - self.app_size[0] + left

And calling:

    Clock.schedule_once(self.do_maximize)

from the App.build() method should approximate what you want.

A more accurate, but more complex approach would be to use subprocess to run a command to capture the device size (something like xdpyinfo on Linux) and parse the output to get the size.

答案2

得分: 0

感谢 @John Anderson,你的解决方案有效,我只需应用一行代码来满足我的需求。

def do_maximize(self, dt):
    self.app_size = Window.size
    Window.maximize()
    Clock.schedule_once(self.adjust_window)

def adjust_window(self, dt):
    window_size = Window.size
    top = Window.top - 50
    left = Window.left
    Window.restore()
    Window.size = (400, 200) # 新的(期望的窗口大小)
    self.app_size = Window.size
    Window.top = window_size[1] - self.app_size[1] + top
    Window.left = window_size[0] - self.app_size[0] + left
英文:

thank you @John Anderson your solution worked, i had only to apply one line for my needs.

def do_maximize(self, dt):
    self.app_size = Window.size
    Window.maximize()
    Clock.schedule_once(self.adjust_window)

def adjust_window(self, dt):
    window_size = Window.size
    top = Window.top - 50
    left = Window.left
    Window.restore()
    Window.size = (400, 200) # new (desired window size)
    self.app_size = Window.size
    Window.top = window_size[1] - self.app_size[1] + top
    Window.left = window_size[0] - self.app_size[0] + left

huangapple
  • 本文由 发表于 2023年3月9日 22:34:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75686001.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定