更改 Kivy 中按钮的大小和位置
Kivy 是Python中一个独立于平台的 GUI 工具。因为它可以在Android、IOS、linux和Windows等平台上运行。它基本上是用来开发Android应用程序的,但这并不意味着它不能在桌面应用程序上使用。
在本文中,我们将看到如何在 kivy Python中更改按钮的大小和位置。有 4 个属性需要设置,按钮的大小和位置。有 2 个属性用于静态放置,另外 2 个用于动态放置。
size : It takes two arguments i.e. (width, height).
Python3
Python3
Python3
button = Button(
text ='Hello world',
size_hint =(.5, .25),
pos =(20, 20))
Python3
button = Button(text ='Hello world', size_hint =(.6, .6),
pos_hint ={'x':.2, 'y':.2})
Python3
## Sample Python application demonstrating the
## How to change button position and size in Kivy.
###################################################
# import modules
import kivy
# base Class of your App inherits from the App class.
# app:always refers to the instance of your application
from kivy.app import App
# creates the button in kivy
# if not imported shows the error
from kivy.uix.button import Button
# This layout allows you to set relative coordinates for children.
from kivy.uix.relativelayout import RelativeLayout
# 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)
# creating the App class
class Pos_Size_App(App):
def build(self):
# A Relative Layout with a size of (300, 300) is created
rl = RelativeLayout(size =(300, 300))
# creating button
# size of button is 20 % by height and width of layout
# position is 'center_x':.7, 'center_y':.5
b1 = Button(size_hint =(.2, .2),
pos_hint ={'center_x':.7, 'center_y':.5},
text ="pos_hint")
# creating button
# size of button is 20 % by height and 50 % width of layout
b2 = Button(size_hint =(.5, .2),
text ="size_hint")
# creating button
# size of button is 20 % by height and width of layout
# position is 200, 200 from bottom left
b3 = Button( size_hint =(.2, .2),
pos =(200, 200),
text ="pos")
# adding button to widget
rl.add_widget(b1)
rl.add_widget(b2)
rl.add_widget(b3)
# returning widget
return rl
# run the App
if __name__ == "__main__":
Pos_Size_App().run()
pos : pos stands for position i.e it is used to position the widget. By default (0, 0), the bottom-left corner of the screen is the default position of the button in kivy python.
Python3
b1 = Button(pos =(100, 100))
b2 = Button(pos =(200, 200))
size_hint : Provide hint of size. It contains two arguments i.e. width and height it can be floating values. By default, all widgets have their size_hint=(1, 1). To create a button 50% of the width and 25% of the height of the layout and positioned at (20, 20), you can do –
Python3
button = Button(
text ='Hello world',
size_hint =(.5, .25),
pos =(20, 20))
pos_hint : Provide hint of position. We can define upto 8 keys i.e. it takes arguments in form of dictionary.
pos_hint = {“x”:1, “y”:1, “left”:1, “right”:1, "center_x":1,
"center_y":1, "top”:1, “bottom”:1("top":0)}
If you want to create a button that will always be the size of layout minus 20% on each side –
Python3
button = Button(text ='Hello world', size_hint =(.6, .6),
pos_hint ={'x':.2, 'y':.2})
Kivy Tutorial – Learn Kivy with Examples.
笔记:
- size_hint 和 pos_hint 只能使用 0-1 之间的值。其中 0 = 0% 和 1 = 100%。
- kivy 中的坐标系从左下角开始工作!这在放置我们的对象时很重要。 (即 (0, 0) 是左下角)。
b1 = Button(size =(100, 100))
b2 = Button(size =(200, 200))
以下是所有 4 个属性的实现:
Python3
b1 = Button(pos =(100, 100))
b2 = Button(pos =(200, 200))
输出: