如何增加屏幕尺寸并将其放在显示器中?

时间:2017-09-21 23:06:55

标签: python python-3.x kivy

我正在制作一个带有多个屏幕的程序,每个屏幕都有自己的大小,因此每次更换屏幕时都必须重新调整应用程序的大小。
问题在于,当我改变窗户的大小时,Kivy只会增加或减少右侧和底侧的长度,而不是四边,因此屏幕总是得到“未经过调整的”#34;改变尺寸后。
我试着用线条做一个例子:

--------------------------------   --------------------------------
|          Monitor             |   |                               |
|           --------           |   |           --------------      |
|          |   My   |          |   |          |              |     |
|          |   App  |          | > |          |              |     |
|           --------           |   |          |              |     |
|                              |   |          |______________|     |
--------------------------------   --------------------------------

就像你可以看到程序只改变右侧和下侧的长度。

如何让屏幕再次居中或更好地改变四边的尺寸,而不仅仅是两个?

这是我的代码示例,但我不知道该怎么做:

这是.py示例:

from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.core.window import Window

class Screen_Manager(ScreenManager):
    pass

class Main(Screen):
    def __init__(self, **kwargs):
        super(Main, self).__init__(**kwargs)

    def on_pre_enter(self):
    Window.size = (900, 500)    


class Login(Screen):
    def __init__(self, **kwargs):
        super(Login, self).__init__(**kwargs)

    def on_pre_enter(self):
        Window.size = (400, 300)    


class MyApp(App):
    def build(self):
        return Screen_Manager()

MyApp().run()

这是一个my.kv示例(my,以便与MyApp一起使用):

<Screen_Manager>:
    id: screen_manager
    Main:
    Login:

<Login>:
    name: 'login'
    Button:
        text: 'Go to Main'
        on_press: root.manager.current = 'main'

<Main>:
    name: 'main'
    Button:
        text: 'Go to Login'
        on_press: root.manager.current = 'login'

我找到了一条路,但我不喜欢它:

    def on_pre_enter(self):
        # New size
        size = (NEW_X, NEW_Y)

        # Get the actual pos and knowing the old size calculate the new one
        top  = Window.top  * Window.size[1] / size[1]
        left = Window.left * Window.size[0] / size[0]

        # Change the size
        Window.size = size

        # Fixing pos
        Window.top  = top
        Window.left = left

2 个答案:

答案 0 :(得分:2)

我找不到任何暗示kivy能够提供主机设备的屏幕尺寸,所以我希望如果有,有人会告诉我们。与此同时,我从另一个论坛借用了一些设置代码。我只是在我的macbook上测试它,它似乎工作,获得屏幕大小。我不知道在Windows,Linux或多个监视器上会发生什么。但这是新代码:

import sys
"""
returns Monitor size x and y in pixels for desktop platforms, or None for
mobile platforms
Found at:
https://groups.google.com/forum/#!topic/kivy-users/uZYrghb87g0
"""
if sys.platform == 'linux2':
    import subprocess
    output = subprocess.Popen(
        'xrandr | grep "\*" | cut -d" " -f4',
        shell=True,
        stdout=subprocess.PIPE).communicate()[0]
    screenx = int(output.replace('\n', '').split('x')[0])
    screeny = int(output.replace('\n', '').split('x')[1])
elif sys.platform == 'win32':
    from win32api import GetSystemMetrics
    screenx = GetSystemMetrics(0)
    screeny = GetSystemMetrics(1)
elif sys.platform == 'darwin':
    from AppKit import NSScreen
    frame_size = NSScreen.mainScreen().frame().size
    screenx = frame_size.width
    screeny = frame_size.height
else:
    # For mobile devices, use full screen
    screenx,screeny = 800,600  # return something

#print "screenx,screeny = ",repr((screenx,screeny))

from kivy.config import Config   # config should appear  before any other kivy stuff
Config.set('graphics','position','custom')

from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.core.window import Window

class Screen_Manager(ScreenManager):
    pass

def center_window(sizex,sizey):
    Window.size = (sizex, sizey)
    Window.left = (screenx - sizex)/2
    Window.top = (screeny - sizey)/2

class Main(Screen):
    def __init__(self, **kwargs):
        super(Main, self).__init__(**kwargs)

    def on_pre_enter(self):
        center_window(900,500)

class Login(Screen):
    def __init__(self, **kwargs):
        super(Login, self).__init__(**kwargs)

    def on_pre_enter(self):
        center_window(400, 300)        

class MultiScreenApp(App):
    def build(self):
        return Screen_Manager()

MultiScreenApp().run()

答案 1 :(得分:0)

我这样做的方法是保存初始Window.center值,并在调整大小事件之后将Window.leftWindow.top值的变化减去。即:

initial_center = None
def build(self):
    self.initial_center = Window.center

有关窗口更改事件:

variation_x = Window.center[0] - self.initial_center[0]
variation_y = Window.center[1] - self.initial_center[1]

Window.left -= variation_x
Window.top -= variation_y

我不是Python / Kivy专家,所以这里可能需要做一些小改进,但是我认为这可以解决您的问题。