Kivy 按钮意外更改值 (size_hint)
Kivy Button changing values unwantedly (size_hint)
所以,我已经研究了 kivy 库几天了,我做了一个简单的应用程序,其中有一些屏幕模拟了 SignIn/Register 环境。我注意到,在我的 .kv 文件中,当我将 "global parameters" 设置为我的小部件时,按钮参数根本没有改变。看看:
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
Gerencia:
transition: FadeTransition()
TelaDeLogin:
TelaDeCadastro:
TelaEsqueci:
TelaEmDesenvolvimento:
<Button>:
size_hint: 1, 0.1
font_size: 40
color: 1, 1, 1, 1
<Label>:
size_hint: 0.5, 0.1
color: 1, 1, 0, 1
font_size: 40
<TextInput>:
multiline: False
size_hint: 0.5, 0.1
<TelaDeLogin>:
name: "Login"
FloatLayout:
Button:
on_release: app.root.current = "Desenvolvimento"
pos_hint: {'x':0, 'y':0.2}
text: 'Logar'
Button:
on_release: app.root.current = "Esqueci"
pos_hint: {'x':0, 'y':0.1}
text: 'Esqueci a senha'
Button:
on_release: app.root.current = "Cadastro"
pos_hint: {'x':0, 'y':0}
text: 'Cadastre-se'
Label:
text: "Usuário"
pos_hint: {'x':0.25, 'y':0.8}
TextInput:
pos_hint: {'x':0.25, 'y':0.7}
Label:
text: "Senha"
pos_hint: {'x':0.25, 'y':0.6}
TextInput:
password: True
pos_hint: {'x':0.25, 'y':0.5}
我忽略了其他一些屏幕,但它们无关紧要,发生的事情是,我做了一些测试并更改了“<"Button">”中的 size_hint 不会影响大小我的按钮,他们显然只是得到一些默认大小。发生的另一件奇怪的事情是,为了测试,我对“<"Button">”和“<"Label">”中的 font_size 以及我放入的值进行了一些更改标签也影响了我在屏幕上的按钮,颜色也是如此。所以我的按钮似乎是从“<"Label">”而不是“<"Button>"”获取它们的值。有人知道发生了什么吗?
说明
您已经覆盖了基础 class 标签,并且 Button 是以下 Kivy 文档中指定的标签。在你的Kivy App中,Button继承了size_hint、font_size和color 来自您的自定义标签。
The Button is a Label with associated actions that are triggered when
the button is pressed (or released after a click/touch). To configure
the button, the same properties (padding, font_size, etc) and sizing
system are used as for the Label class
解决方案
为标签和按钮创建动态 classes。
- 创建一个继承自 Button 的动态 class。将实例化的 children、
Button:
替换为 MyButton:
- 创建一个继承自 Label 的动态 class。将实例化的 children、
Label:
替换为 MyLabel:
片段
<MyButton@Button>:
size_hint: 1, 0.1
font_size: 40
color: 1, 1, 1, 1
<MyLabel@Label>:
size_hint: 0.5, 0.1
color: 1, 1, 0, 1
font_size: 40
...
<TelaDeLogin>:
name: "Login"
FloatLayout:
MyButton:
on_release: app.root.current = "Desenvolvimento"
pos_hint: {'x':0, 'y':0.2}
text: 'Logar'
MyButton:
on_release: app.root.current = "Esqueci"
pos_hint: {'x':0, 'y':0.1}
text: 'Esqueci a senha'
MyButton:
on_release: app.root.current = "Cadastro"
pos_hint: {'x':0, 'y':0}
text: 'Cadastre-se'
MyLabel:
text: "Usuário"
pos_hint: {'x':0.25, 'y':0.8}
TextInput:
pos_hint: {'x':0.25, 'y':0.7}
MyLabel:
text: "Senha"
pos_hint: {'x':0.25, 'y':0.6}
TextInput:
password: True
pos_hint: {'x':0.25, 'y':0.5}
输出
所以,我已经研究了 kivy 库几天了,我做了一个简单的应用程序,其中有一些屏幕模拟了 SignIn/Register 环境。我注意到,在我的 .kv 文件中,当我将 "global parameters" 设置为我的小部件时,按钮参数根本没有改变。看看:
#: import FadeTransition kivy.uix.screenmanager.FadeTransition
Gerencia:
transition: FadeTransition()
TelaDeLogin:
TelaDeCadastro:
TelaEsqueci:
TelaEmDesenvolvimento:
<Button>:
size_hint: 1, 0.1
font_size: 40
color: 1, 1, 1, 1
<Label>:
size_hint: 0.5, 0.1
color: 1, 1, 0, 1
font_size: 40
<TextInput>:
multiline: False
size_hint: 0.5, 0.1
<TelaDeLogin>:
name: "Login"
FloatLayout:
Button:
on_release: app.root.current = "Desenvolvimento"
pos_hint: {'x':0, 'y':0.2}
text: 'Logar'
Button:
on_release: app.root.current = "Esqueci"
pos_hint: {'x':0, 'y':0.1}
text: 'Esqueci a senha'
Button:
on_release: app.root.current = "Cadastro"
pos_hint: {'x':0, 'y':0}
text: 'Cadastre-se'
Label:
text: "Usuário"
pos_hint: {'x':0.25, 'y':0.8}
TextInput:
pos_hint: {'x':0.25, 'y':0.7}
Label:
text: "Senha"
pos_hint: {'x':0.25, 'y':0.6}
TextInput:
password: True
pos_hint: {'x':0.25, 'y':0.5}
我忽略了其他一些屏幕,但它们无关紧要,发生的事情是,我做了一些测试并更改了“<"Button">”中的 size_hint 不会影响大小我的按钮,他们显然只是得到一些默认大小。发生的另一件奇怪的事情是,为了测试,我对“<"Button">”和“<"Label">”中的 font_size 以及我放入的值进行了一些更改标签也影响了我在屏幕上的按钮,颜色也是如此。所以我的按钮似乎是从“<"Label">”而不是“<"Button>"”获取它们的值。有人知道发生了什么吗?
说明
您已经覆盖了基础 class 标签,并且 Button 是以下 Kivy 文档中指定的标签。在你的Kivy App中,Button继承了size_hint、font_size和color 来自您的自定义标签。
The Button is a Label with associated actions that are triggered when the button is pressed (or released after a click/touch). To configure the button, the same properties (padding, font_size, etc) and sizing system are used as for the Label class
解决方案
为标签和按钮创建动态 classes。
- 创建一个继承自 Button 的动态 class。将实例化的 children、
Button:
替换为MyButton:
- 创建一个继承自 Label 的动态 class。将实例化的 children、
Label:
替换为MyLabel:
片段
<MyButton@Button>:
size_hint: 1, 0.1
font_size: 40
color: 1, 1, 1, 1
<MyLabel@Label>:
size_hint: 0.5, 0.1
color: 1, 1, 0, 1
font_size: 40
...
<TelaDeLogin>:
name: "Login"
FloatLayout:
MyButton:
on_release: app.root.current = "Desenvolvimento"
pos_hint: {'x':0, 'y':0.2}
text: 'Logar'
MyButton:
on_release: app.root.current = "Esqueci"
pos_hint: {'x':0, 'y':0.1}
text: 'Esqueci a senha'
MyButton:
on_release: app.root.current = "Cadastro"
pos_hint: {'x':0, 'y':0}
text: 'Cadastre-se'
MyLabel:
text: "Usuário"
pos_hint: {'x':0.25, 'y':0.8}
TextInput:
pos_hint: {'x':0.25, 'y':0.7}
MyLabel:
text: "Senha"
pos_hint: {'x':0.25, 'y':0.6}
TextInput:
password: True
pos_hint: {'x':0.25, 'y':0.5}