問題描述
我正在使用 kivy 為應用程序制作一個非常簡單的 gui.沒有什么復雜的,非常簡單的布局.
I am using kivy to make a very simple gui for an application. Nothing complex, very simple layout.
盡管如此,我在使用 TextInputs 時遇到了困難……它們總是以全高顯示,我無法讓它們調整為合理"的文本高度,例如高度.
Nevertheless I am having a hard time with TextInputs...They always display with full height and I can't manage to make them adjust to a "reasonable" text-height like height.
我使用 kv 文件樣式,因為我發現它更簡潔,更容易將其集成到現有應用程序中...我想盡可能減少應用程序的 gui-python 代碼.
I am using the kv files style since I find it cleaner and easier to integrate it in an already existing app...I would like to reduce as much as possible the gui-python code of the app.
這是我為 TextInput 得到的(無用添加 gui 的其他部分).
Here is what I got for the TextInput (useless to add other parts of the gui).
Python 代碼
# textInput.py
from kivy import require
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang.builder import Builder
Builder.load_file('path/to/kv/file/textInput.kv')
require('1.10.0')
class MainScreen(BoxLayout):
pass
class Test(App):
def build(self):
self.title = 'Testing textInput'
return MainScreen()
if __name__ == '__main__':
Test().run()
KV 代碼
# textInput.kv
<MainScreen>
orientation: 'vertical'
# Third section title
Label:
size_hint: (1, .1)
text: 'Setup Connection'
font_size: 25
# Third section Box
BoxLayout:
size_hint: (1, .2)
padding: [100, 0, 100, 0]
BoxLayout:
Label:
size_hint: (.2, 1)
text: 'Host'
TextInput:
height: self.minimum_height
multiline: False
text: 'localhost'
Label:
size_hint: (.2, 1)
text: ''
Label:
size_hint: (.2, 1)
text: 'Port'
TextInput:
size_hint: (.2, 1)
multiline: False
text: '502'
我嘗試了很多東西,在這里的代碼中我嘗試同時使用 size_hint 和 height...但沒有一個有效..
I have tried lot of stuff, in the code here I am trying both to use size_hint and height...but none works..
推薦答案
要設置一個小部件的高度,首先將 size_hint_y
設置為 None
然后你可以設置height
到任何你想要的.
To set a height of a widget, first set the size_hint_y
to None
and then you can set the height
to whatever you want.
TextInput:
size_hint: (.2, None)
height: 30
multiline: False
text: '502'
這篇關于Python kivy - 如何減少 TextInput 的高度的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!