如何在kivy上获得文本输入功能?

2024-04-25 12:37:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我不知道如何接近下面的基维方面。我想使用kivy来获取输入值,而不是使用命令提示符来获取输入值。如果你能帮我,我将不胜感激。例如,第一行向用户提问。用户回答了这个问题,这个信息就变成了一个变量,我可以用这个变量进行操作。你知道吗

    def coord(d):
        while True:
            deforchan = input('Use default(d) coordinates or change(c) in page %s: ' %d)
            if deforchan == 'd':
                x1, y1, x2, y2 = '700','800','1140','200'
                i = table_foo(x1, y1, x2, y2,d)
                break
            elif deforchan == 'c':
                print('Enter (0,0) coordinate: ')
                x1, y1 = input('x_coordinate: '), input('y_coordinate: ')
                print('Enter (0,0) coordinate:')
                x2, y2 = input('x_coordinate: '), input('y_coordinate: ')
                x1 = str(x1)
                y1 = str(850-int(y1))
                x2 = str(x2)
                y2 = str(850-int(y2))
                print(x1, y1, x2, y2,d)
                i = table_foo(x1, y1, x2, y2,d)
                break
            else:
                print('Oops! Try Again')

        return x1, y1, x2, y2, d

    def final():
    #     pdfname = 'example2.pdf'
    #     nofpages = len(PdfReader(pdfname).pages)
        for d in range(1, int(nofpages)+1):
            x1, y1, x2, y2, d = coord(d)

            while True:
                add_tables = input('Any additional tables in page %s? (y or n) ' %d)
                if add_tables == 'y':
                    x1, y1, x2, y2, d = coord(d)
                elif add_tables == 'n':
                    break
                else:
                    print('Oops! Try Again')

Tags: inaddcoordinateinputtablesintprintx1
1条回答
网友
1楼 · 发布于 2024-04-25 12:37:33

使用Kivy小部件如BoxLayoutGridLayoutLabelTextInputButton来显示默认值,并等待Use重写默认值或输入新值。你知道吗

你知道吗主.py你知道吗

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.lang import Builder


Builder.load_string("""
<Demo>:
    orientation: 'vertical'

    GridLayout:
        cols: 3
        Label:
            text: 'Enter (0,0) coordinate:'
        TextInput:
            id: x1
            hint_text: 'x1 coordinate'
            text: '700'
        TextInput:
            id: y1
            hint_text: 'y1 coordinate'
            text: '800'
        Label:
            text: 'Enter (0,0) coordinate:'
        TextInput:
            id: x2
            hint_text: 'x2 coordinate'
            text: '1140'
        TextInput:
            id: y2
            hint_text: 'y2 coordinate'
            text: '200'

    Button:
        text: 'Submit'
        on_release: app.root.coordinates(x1.text, y1.text, x2.text, y2.text)

""")


class Demo(BoxLayout):

    def coordinates(self, x1, y1, x2, y2):
        print(f"x1={x1}, y1={y1}, x2={x2}, y2={y2}")


class TestApp(App):

    def build(self):
        return Demo()


if __name__ == "__main__":
    TestApp().run()

输出

Result

相关问题 更多 >