我想用 Kivy 写一个简单的 GUI。我使用 *.kv 文件设计 GUI,但遇到问题。我的元素之间总是有间隙(见图,我将间隙涂成红色)。谁能告诉我,我该如何更改 *.kv 文件?
我不喜欢有间隙的 GUI:
如果我用鼠标更改窗口大小或在代码中设置窗口大小,则一切在我的电脑上看起来都很好。但我也想在其他系统上使用该应用程序。所以它看起来应该到处都很好。
这是我的代码:
主.py
#!/usr/bin/env python3
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.widget import Widget
class CalculationScreen(Screen):
pass
class AnimWidget(Widget):
pass
class Calculation(App):
def build(self):
screenmanager = ScreenManager()
screenmanager.add_widget(CalculationScreen(name='calculation'))
return screenmanager
def main():
kv = Builder.load_file("calculation.kv")
Calculation().run()
if __name__ == '__main__':
main()
和 *.kv 文件:
# Filename: calculation.kv
<CalculationScreen>:
GridLayout:
rows: 2
cols: 2
GridLayout:
rows: 5
cols: 3
Label:
text: "First one"
size_hint: 1, None
height: 35
TextInput:
size_hint: 1, None
height: 35
Label:
text: "[ABC]"
size_hint: 1, None
height: 35
Label:
text: "Next one"
size_hint: 1, None
height: 35
TextInput:
size_hint: 1, None
height: 35
Label:
text: "[DFG]"
size_hint: 1, None
height: 35
Label:
text: "Something else"
size_hint: 1, None
height: 35
TextInput:
size_hint: 1, None
height: 35
Label:
text: "[KV]"
size_hint: 1, None
height: 35
Label:
text: "Another one"
size_hint: 1, None
height: 35
TextInput:
size_hint: 1, None
height: 35
Label:
text: "[XY]"
size_hint: 1, None
height: 35
Label:
text: "The Last"
size_hint: 1, None
height: 35
TextInput:
size_hint: 1, None
height: 35
Label:
text: "[ZZ]"
size_hint: 1, None
height: 35
GridLayout:
cols: 1
rows: 1
Image:
source: 'toad.jpg'
size_hint: 1, None
height: 140
GridLayout:
cols: 1
rows: 2
GridLayout:
cols: 4
rows: 1
Label:
text: 'First'
size_hint_y: None
CheckBox:
id: 'first'
size_hint_y: None
Label:
text: 'Second'
size_hint_y: None
CheckBox:
id: 'second'
size_hint_y: None
Button:
size_hint: 0.4, 0.4
pos_hint: {'x': 1, 'y': 1}
text: 'Calculate'
GridLayout:
rows: 2
cols: 1
#size_hint_y: None
Label:
id: 'result'
text: 'Result'
Label:
text: 'Version 1.0'
text_size: self.size
halign: "right"
向您寻求帮助!
我认为你可以添加间距:(0, 0) 2.首先网格布局GridLayout: 行数:2 列数:2 space: (0, 0) # 添加此行以消除间隙
GridLayout:
rows: 5
cols: 3
spacing: (0, 0) # Add this line to remove the gap
Label:
text: "First one"
size_hint: 1, None
height: 35