Kivy buttons and functions and converting from tkinter

39 views Asked by At

Im new to kivy and python I created a app with tkinter and can run it on my pc but really need to use it on my mobile devices. later found out cant convert tkinter into apk or ios so that messed me up lol.

enter image description here

When I click on my buttons in tkinter it gets the input from sales does some calculations then outputs to the space under each label

each button outputs to 3 spots how can I do this in kivy? do I do it in the KV file or should I write my code in python and if so how to I call that function in kv file for gui?

Sorry I know complete noob question with 1000 possible answers

Thanks Jordan

1

There are 1 answers

0
Mark On

here is a template that can get you started. it is divided into test.py and test.kv

from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.lang import Builder


# this object name should match the .kv file object
class MyBoxLayout(BoxLayout):
    my_textinput: TextInput

    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.my_textinput.text = "hello"


class MyApp(App):
    def build(self):
        return self._main

    def button_pressed(self, my_button: Button, number: int, *args, **kwargs):
        print(type(my_button))
        self._main.my_textinput.text = f"button {number}"

    def __init__(self):
        super().__init__()
        Builder.load_file("test.kv")
        self._main = MyBoxLayout()


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

and the .kv file

<MyBoxLayout>:
    orientation: "vertical"
    padding: 20

    # python on this side: Kivy id on this side
    my_textinput: my_textinput
    Label:
        text: "Example application"
        font_size: 24
        size_hint_y: None
        height: 50

    TextInput:
        id: my_textinput
        hint_text: "Enter text here"

    BoxLayout:
        size_hint_y: 2

        Button:
            text: "Button 1"
            on_press: app.button_pressed(self, 1)

        Button:
            text: "Button 2"
            on_press: app.button_pressed(self, 2)

        Button:
            text: "Button 3"
            on_press: app.button_pressed(self, 3)