Python,Kivy:多个复选框干扰用户界面
Python, Kivy: Multiple Checkboxes Interfere With User Interface
我正在为自己创建一个密码生成器,我已经 运行 解决了这个问题。我放了两个复选框和一个文本输入。无论我在屏幕上的哪个位置单击,它 deactivates/activates 都是最底部的复选框。你可以 运行 我的代码(这是完整的代码,没有任何例外),看看会发生什么。
.py
from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
import string
sm = ScreenManager()
class main(Screen):
def generate(self):
include_num = self.ids.include_num.active
include_special = self.ids.include_special.active
letters = list(string.ascii_letters)
special = list(string.punctuation)
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
if self.ids.len_of_password.text:
pass
else:
self.ids.len_of_password.text = 'NEEDED'
class MyApp(App):
def build(self):
sm.add_widget(main(name='main'))
return sm
if __name__ == '__main__':
MyApp().run()
^^ 因为问题我还没把逻辑放上去,请不要着急给我逻辑。相反,我正在寻找多个复选框问题的解决方案。
.kv
<main>
FloatLayout:
Label:
text: 'Length:'
font_size: 70
pos_hint: {'right': 0.85,'y': 0.3}
TextInput:
multiline: False
id: len_of_password
text: '20'
size_hint: 0.3, 0.15
font_size: self.height / 4 * 3
pos_hint: {'x': 0.55, 'y': 0.725}
Label:
text: 'Include numbers?'
font_size: 45
pos_hint: {'right': 0.85, 'y': 0.1}
CheckBox:
active: True
pos_hint: {'x': 0.2, 'y': 0.1}
id: include_num
Label:
text: 'Include special characters?'
font_size: 35
pos_hint: {'right': 0.85, 'top': 0.95}
CheckBox:
active: True
pos_hint: {'x': 0.2, 'top': 0.95}
id: include_special
Button:
text: 'Generate'
font_size: (self.height - len(self.text) * 2) / 2
size_hint: 0.5, 0.2
pos_hint: {'x': 0.25, 'y': 0.1}
on_release: root.generate()
感谢帮助!
问题是 CheckBox
填满了整个 main
Screen
。发生这种情况是因为默认的 size_hint
是 (1,1)
。尝试为 kv
.
中的所有小部件设置 size_hint
我正在为自己创建一个密码生成器,我已经 运行 解决了这个问题。我放了两个复选框和一个文本输入。无论我在屏幕上的哪个位置单击,它 deactivates/activates 都是最底部的复选框。你可以 运行 我的代码(这是完整的代码,没有任何例外),看看会发生什么。
.py
from kivy.app import App
from kivy.uix.screenmanager import Screen, ScreenManager
import string
sm = ScreenManager()
class main(Screen):
def generate(self):
include_num = self.ids.include_num.active
include_special = self.ids.include_special.active
letters = list(string.ascii_letters)
special = list(string.punctuation)
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
if self.ids.len_of_password.text:
pass
else:
self.ids.len_of_password.text = 'NEEDED'
class MyApp(App):
def build(self):
sm.add_widget(main(name='main'))
return sm
if __name__ == '__main__':
MyApp().run()
^^ 因为问题我还没把逻辑放上去,请不要着急给我逻辑。相反,我正在寻找多个复选框问题的解决方案。
.kv
<main>
FloatLayout:
Label:
text: 'Length:'
font_size: 70
pos_hint: {'right': 0.85,'y': 0.3}
TextInput:
multiline: False
id: len_of_password
text: '20'
size_hint: 0.3, 0.15
font_size: self.height / 4 * 3
pos_hint: {'x': 0.55, 'y': 0.725}
Label:
text: 'Include numbers?'
font_size: 45
pos_hint: {'right': 0.85, 'y': 0.1}
CheckBox:
active: True
pos_hint: {'x': 0.2, 'y': 0.1}
id: include_num
Label:
text: 'Include special characters?'
font_size: 35
pos_hint: {'right': 0.85, 'top': 0.95}
CheckBox:
active: True
pos_hint: {'x': 0.2, 'top': 0.95}
id: include_special
Button:
text: 'Generate'
font_size: (self.height - len(self.text) * 2) / 2
size_hint: 0.5, 0.2
pos_hint: {'x': 0.25, 'y': 0.1}
on_release: root.generate()
感谢帮助!
问题是 CheckBox
填满了整个 main
Screen
。发生这种情况是因为默认的 size_hint
是 (1,1)
。尝试为 kv
.
size_hint