如何增加屏幕尺寸并将其居中放置在显示器中? [英] How to increase size of a screen and center it in the monitor?

查看:164
本文介绍了如何增加屏幕尺寸并将其居中放置在显示器中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个具有多个屏幕的程序,每个屏幕都有自己的大小,因此每次更换屏幕时,都必须重新调整应用程序的大小.
问题是,当我更改窗口的大小时,Kivy仅增加或减少了右侧和底部的长度,而不是四个侧面的长度,因此更改尺寸后,屏幕始终会变得未居中".
我将尝试用行作为示例:

I am making a program with several screens and each one has its own size, so each time I change of screen I must re-size the App.
The problem is that, when I change the size of the window, Kivy only increase or reduce the lenght of the right and the bottom sides, not the four sides, so the screen always get "uncentered" after change the size.
I'll try to make an example with lines:

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

就像您看到的一样,该程序仅更改了右侧和底部的长度.

Like you can see the program only change the lengh of the right and bottom sides.

我如何才能再次将屏幕居中,或者更好地更改四个侧面而不是两个侧面的大小?

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

This is an example of my code but I don't have any idea of what to do:

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示例(为了与MyApp配合使用,my):

This is an my.kv example (my in order to work with 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'

我找到了一种方法,但是我不太喜欢它:

I have found a way but I don't like it much:

    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

推荐答案

我找不到任何迹象表明kivy可以提供主机设备的屏幕尺寸,所以我希望如果有,那么有人会告诉我们.同时,我从另一个论坛借来了一些安装代码.我只在Macbook上进行了测试,它似乎可以正常工作,并且可以得到屏幕尺寸.我不知道在Windows,Linux或多台显示器上会发生什么.但是,这是新代码:

I couldn't find any hint that kivy was able to supply the screen size of the host device, so I hope if there is one, someone will tell us. In the meantime, I borrowed some setup code from another forum. I've only tested it on my macbook, and it seems to work, getting the screen size. I don't know what will happen on windows, or linux, or multiple monitors. But here's the new code:

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

这篇关于如何增加屏幕尺寸并将其居中放置在显示器中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆