📜  Python| Kivy中的窗口大小调整

📅  最后修改于: 2022-05-13 01:55:42.072000             🧑  作者: Mango

Python| Kivy中的窗口大小调整

Kivy 是Python中一个独立于平台的 GUI 工具。由于它可以在 Android、IOS、linux 和 Windows 等平台上运行。Kivy 为您提供了一次编写代码并在不同平台上运行的功能。基本上是用来开发Android应用的,但不代表不能用在Desktops应用上。
Kivy 是一个大小无关紧要的平台,因为它会相应地进行自我调整,但是如果我们想在某种程度上固定大小,无论是高度还是宽度还是无边界,都取决于用户的要求。

在本文中,我们将看到在 kivy 中调整窗口大小的三种格式。
注意:只需注意窗口输出中的大小,并在窗口可见或不可见时检查窗口上的最小化和最大化按钮。
对于调整窗口大小,我们有:

from kivy.config import Config

Kivy 有一个配置文件,用于确定默认设置。为了更改这些设置,您可以手动更改此文件或使用 Config 对象。
配置选项控制应用程序的初始化。为了避免在创建窗口之前配置设置不起作用或未应用的情况(例如设置初始窗口大小),应在导入任何其他 Kivy 模块之前使用Config.set 。理想情况下,这意味着在 main.py 脚本的开头设置它们。
当没有固定窗口大小时,即根据用户完全调整大小:

Python3
# To change the kivy default settings
# we use this module config
from kivy.config import Config
 
# 0 being off 1 being on as in true / false
# you can use 0 or 1 && True or False
Config.set('graphics', 'resizable', True)
 
# import kivy module
import kivy
 
# this restrict the kivy version i.e
# below this kivy version you cannot use the app
kivy.require("1.9.1")
 
# base Class of your App inherits from the App class.
# app:always refers to the instance of your application
from kivy.app import App
 
# if you not import label and use it through error
from kivy.uix.label import Label
 
# defining the App class
class MyLabelApp(App):
    def build(self):
        # label display the text on screen
        # markup text with different colour
        l2 = Label(text ="[color = ff3333][b]Hello !!!!!!!!!!![/b]
                   [/color]\n [color = 3333ff]GFG !!:):):):)[/color]",
                   font_size ='20sp', markup = True)    
        return l2
     
# creating the object
label = MyLabelApp()
 
# run the window
label.run()


Python3
# To change the kivy default settings
# we use this module config
from kivy.config import Config
 
# 0 being off 1 being on as in true / false
# you can use 0 or 1 && True or False
Config.set('graphics', 'resizable', '0')
 
# fix the width of the window
Config.set('graphics', 'width', '500')


Python3
# to change the kivy default settings
# we use this module config
from kivy.config import Config
 
# 0 being off 1 being on as in true / false
# you can use 0 or 1 && True or False
Config.set('graphics', 'resizable', '0')
 
# fix the height of the window
Config.set('graphics', 'height', '400')


Python3
# to change the kivy default settings
# we use this module config
from kivy.config import Config
 
# 0 being off 1 being on as in true / false
# you can use 0 or 1 && True or False
Config.set('graphics', 'resizable', '0')
 
# fix the width of the window
Config.set('graphics', 'width', '500')
 
# fix the height of the window
Config.set('graphics', 'height', '500')


输出:


没有调整大小,固定大小与宽度:

Python3

# To change the kivy default settings
# we use this module config
from kivy.config import Config
 
# 0 being off 1 being on as in true / false
# you can use 0 or 1 && True or False
Config.set('graphics', 'resizable', '0')
 
# fix the width of the window
Config.set('graphics', 'width', '500')

输出:


固定窗口的高度:

Python3

# to change the kivy default settings
# we use this module config
from kivy.config import Config
 
# 0 being off 1 being on as in true / false
# you can use 0 or 1 && True or False
Config.set('graphics', 'resizable', '0')
 
# fix the height of the window
Config.set('graphics', 'height', '400')

输出:


我们可以同时使用高度和宽度限制:

Python3

# to change the kivy default settings
# we use this module config
from kivy.config import Config
 
# 0 being off 1 being on as in true / false
# you can use 0 or 1 && True or False
Config.set('graphics', 'resizable', '0')
 
# fix the width of the window
Config.set('graphics', 'width', '500')
 
# fix the height of the window
Config.set('graphics', 'height', '500')

输出: