[英]Insert Text to TextInput in Kivy using .kv File
Is there any way that you can insert text into a Text Input field similar to Tkinter E1.insert()
(And also a way to clear text input fields)?有没有什么方法可以将文本插入类似于 Tkinter
E1.insert()
的文本输入字段(还有一种清除文本输入字段的方法)? I am trying to make a Board Foot Calculator app.我正在尝试制作一个 Board Foot Calculator 应用程序。 My code is shown below for both.kv file and also the.py file.
我的代码在下面显示了 .kv 文件和 .py 文件。
Thank you so much!太感谢了! I appreciate it!
我很感激!
Python File Code: Python 档案编号:
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
Builder.load_file('design.kv')
answers = []
class CalcScreen(Screen):
def list_view(self):
self.manager.current = "list_screen"
def calculate(self):
LengthVal = float(self.ids.length.text)
WidthVal = float(self.ids.width.text)
ThicknessVal = float(self.ids.thickness.text)
FinalCalc = LengthVal * WidthVal * ThicknessVal / 144
FinalCalc = round(FinalCalc,2)
answers.append(FinalCalc)
# Insert text into TextInput
class ListScreen(Screen):
def calc_view(self):
self.manager.current = "calc_screen"
class RootWidget(ScreenManager):
pass
class MainApp(App):
def build(self):
return RootWidget()
if __name__ == "__main__":
MainApp().run()
<CalcScreen>:
GridLayout:
cols: 1
GridLayout:
cols: 1
Label:
text: "Board Foot Calculator"
TextInput:
id: length
hint_text: "Length in Inches"
TextInput:
id: width
hint_text: "Width in Inches"
TextInput:
id: thickness
hint_text: "Thickness in Inches"
Button:
text: "Calculate"
on_press: root.calculate()
Button:
text: "Clear"
TextInput:
id: board_feet
hint_text: "Board Feet"
Button:
text: "List View"
on_press: root.list_view()
<ListScreen>:
GridLayout:
cols: 1
GridLayout:
cols: 1
Label:
text: "Board Foot Calculator - List"
TextInput:
id: list
hint_text: "List"
TextInput:
id: total_board_feet
hint_text: "Total Board Feet"
TextInput:
id: total_boards
hint_text: "Total Boards"
Button:
text: "Clear List"
Button:
text: "Back"
on_press: root.calc_view()
<RootWidget>
CalcScreen:
name: "calc_screen"
ListScreen:
name: "list_screen"
In your kv
file you can clear the TextInputs
like this:在您的
kv
文件中,您可以像这样清除TextInputs
:
Button:
text: "Clear"
on_release:
length.text = ''
width.text = ''
thickness.text = ''
And you can set the text of the board_feet
TextInput
by modifying your calculate()
method:您可以通过修改
calculate()
方法来设置board_feet
TextInput
的文本:
def calculate(self):
LengthVal = float(self.ids.length.text)
WidthVal = float(self.ids.width.text)
ThicknessVal = float(self.ids.thickness.text)
FinalCalc = LengthVal * WidthVal * ThicknessVal / 144
FinalCalc = round(FinalCalc,2)
answers.append(FinalCalc)
self.ids.board_feet.text = str(FinalCalc)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.