【问题标题】:Insert Text to TextInput in Kivy using .kv File使用 .kv 文件在 Kivy 中将文本插入到 TextInput
【发布时间】:2020-08-20 03:07:43
【问题描述】:

有什么方法可以将文本插入到类似于 Tkinter E1.insert() 的文本输入字段中(还有一种清除文本输入字段的方法)?我正在尝试制作一个板脚计算器应用程序。 .kv 文件和 .py 文件的代码如下所示。

非常感谢!我很感激!

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()

Kivy 文件代码:

<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"

【问题讨论】:

  • 我也刚刚在帖子中添加了 .kv 代码。
  • 您可以使用insert_text() 插入文本。您可以使用ti.text = '' 清除文本(其中tiTextInput 实例)。
  • 那是用.kv文件吗?
  • 还有我需要在()里面放什么
  • 不,这不在kv 文件中。

标签: python kivy


【解决方案1】:

在您的kv 文件中,您可以像这样清除TextInputs

        Button:
            text: "Clear"
            on_release:
                length.text = ''
                width.text = ''
                thickness.text = ''

您可以通过修改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)

【讨论】:

  • 好的!那行得通!但是,有没有办法不删除前一个并将其留在那里?
  • 只需使用+= 而不是=
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-12
  • 2021-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多