简体   繁体   中英

How to change size of widget when I change window size in Kivy?

I am a python beginner trying to do a calculator. I need help with one problem. When I run the application and resize the window by dragging the mouse, the widgets do not adjust to the size of the window and are behind the end of the window. I tried to return FloatLayout instead of the class KalkulackaLayout and it worked the way I wanted. But when I returned the class KalkulackaLayout, it didn't work and I need to return this class.

Python file:

from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.widget import Widget

class KalkulackaLayout(Widget):
    def button_cislo(self, cislo):
        pred = self.ids.kalkulacka_input.text

        if pred == "0":
            self.ids.kalkulacka_input.text = ""
            self.ids.kalkulacka_input.text = f"{cislo}"

        else:
            self.ids.kalkulacka_input.text = f"{pred}{cislo}"

    def znamenko(self, znamenko):
        pred = self.ids.kalkulacka_input.text
        self.ids.kalkulacka_input.text = f"{pred}{znamenko}"

    def vymazat(self):
        self.ids.kalkulacka_input.text = ""

    def vypocitej(self):
        pred = self.ids.kalkulacka_input.text
        try:
            vysledek = eval(pred)
            self.ids.kalkulacka_input.text = str(vysledek)
        except:
            self.ids.kalkulacka_input.text = "ERROR!"

    pass

class Kalkulacka(App):
    def build(self):
        return KalkulackaLayout()

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

.kv file:

<KalkulackaLayout>
    FloatLayout:
        size:300,300
        Button:
            text: "1"
            size_hint:0.5,0.5
            pos_hint: {"bottom":0.1}
            on_press: root.button_cislo(1)

        Button:
            text: "2"
            size_hint:0.5,0.5
            pos_hint:{"right": 1, "bottom": 0.1}
            on_press: root.button_cislo(2)

        Button:
            text: "3"
            size_hint:0.5,0.5
            pos_hint:{"right": 1.5, "bottom": 0.1}
            on_press: root.button_cislo(3)

        Button:
            text: "÷"
            size_hint:0.5,0.5
            pos_hint:{"right": 2, "bottom": 0.1}
            on_press: root.znamenko("/")

        Button:
            text: "-"
            size_hint:0.5,0.5
            pos_hint:{"right": 2.5, "bottom": 0.1}
            on_press: root.znamenko("-")

        Button:
            text: "4"
            size_hint:0.5,0.5
            pos_hint:{"top": 1}
            on_press: root.button_cislo(4)

        Button:
            text: "5"
            size_hint:0.5,0.5
            pos_hint:{"right": 1, "top": 1}
            on_press: root.button_cislo(5)

        Button:
            text: "6"
            size_hint:0.5,0.5
            pos_hint:{"right": 1.5, "top": 1}
            on_press: root.button_cislo(6)

        Button:
            text: "x"
            size_hint:0.5,0.5
            pos_hint:{"right": 2, "top": 1}
            on_press: root.znamenko("*")

        Button:
            text: "+"
            size_hint:0.5,0.5
            pos_hint:{"right": 2.5, "top": 1}
            on_press: root.znamenko("+")

        Button:
            text: "7"
            size_hint:0.5,0.5
            pos_hint:{"top": 1.5}
            on_press: root.button_cislo(7)

        Button:
            text: "8"
            size_hint:0.5,0.5
            pos_hint:{"right": 1, "top": 1.5}
            on_press: root.button_cislo(8)

        Button:
            text: "9"
            size_hint:0.5,0.5
            pos_hint:{"right": 1.5, "top": 1.5}
            on_press: root.button_cislo(9)

        Button:
            text: "="
            size_hint:0.5,0.5
            pos_hint:{"right": 2, "top": 1.5}
            on_press: root.vypocitej()

        Button:
            text: "C"
            size_hint:0.5,0.5
            pos_hint:{"right": 2.5, "top": 1.5}
            on_press: root.vymazat()

        TextInput:
            text: ""
            size_hint: 2.667,0.25
            font_size: 60
            pos_hint: {"top":2, "right":2.667}
            id: kalkulacka_input
class KalkulackaLayout(Widget):

Instead of inheriting from Widget, inherit from a layout class like BoxLayout that will automatically reposition its children when its own size changes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM