我试图使用 python kivy 制作 calcapp 但一开始就崩溃了

时间:2021-07-01 08:10:25

标签: python android kivy calculator

这是我的 .py 文件

from kivy.app import App
from kivy.uix.widget import Widget
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen, ScreenManager
from kivy.core.window import Window
from kivy.uix.button import Button

Window.size = (500,700)
Builder.load_file('calc.kv')

class MyLayout(Widget):
    pass

class Screen1(Screen):
     def clear (self):
         self.ids.calc_input.text = "0"
     def button_press(self, button):
         prior = self.ids.calc_input.text
         if prior == "0":
             self.ids.calc_input.text = ''
             #self.ids.calc_input.text = f'{Button}'
         else:
             self.ids.calc_input.text = f'{prior}{button}'
     def  math_sign(self, sign):
         prior = self.ids.calc_input.text
         self.ids.calc_input.text = f'{prior}{sign}'

     def remove(self):
          prior = self.ids.calc_input.text
          prior = prior [:-1]
          self.ids.calc_input.text = prior

     def dot(self):
         prior = self.ids.calc_input.text 
         num_list = prior.split("+")
         num_list[-1]
         if "+" in prior and "." not in num_list[-1]:
            prior = f'{prior}.'
         self.ids.calc_input.text = prior                  
         if "." in prior:
            pass
         else:
            prior = f'{prior}.'
         self.ids.calc_input.text = prior

     def equals(self):
            prior = self.ids.calc_input.text
            answer = eval(prior)
            self.ids.calc_input.text = str(answer) 


            '''
            if "+" in prior:
             num_list = prior.split("+")
             answer = 0.0
             for number in num_list:
                 answer = answer + float(number)
                 
                 
                 self.ids.calc_input.text = str(answer)
                 
            if "-" in prior:
             num_list = prior.split("-")
             answer = 0.0
             for number in num_list:
                 answer = answer - float(number)
                 
                 
                 self.ids.calc_input.text = str(answer)
                 
            if "x" in prior:
             num_list = prior.split("*")
             answer = 0.0
             for number in num_list:
                 answer = answer * float(number)
                 
                 
                 self.ids.calc_input.text = str(answer)
                 
            if "/" in prior:
             num_list = prior.split("/")
             answer = 0.0
             for number in num_list:
                 answer = answer / float(number)
                 
                 
                 self.ids.calc_input.text = str(answer)
                 
                 #self.ids.text = answer
            '''
class WindowManager(ScreenManager):
    pass

class App(App):
    def build(self):
        return WindowManager()

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

这是我的 .kv 文件

#:kivy 2.0.0
<WindowManager>:
    Screen1


<Screen1>:
    BoxLayout:
        orientation:"vertical"
        size: root.width, root.height

        TextInput:
            id: calc_input
            text: "0"
            halign: "right"
            font_size : 65
            size_hint: (1, .15)

        GridLayout:
            cols:4
            rows:5

#            Button:
#                size_hint: (.2, .2)
#                font_size: 32
#                text: "%"

            Button:

                size_hint: (.2, .2)
                font_size: 32
                text: "<<<"
                on_press : root.remove()


            Button:
                id: clear
                size_hint: (.2, .2)
                font_size: 32
                text: "C"
                on_press : root.clear()

            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "/"
                on_press : root.math_sign("/")


            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "7"
                on_press : root.button_press(7)

            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "8"
                on_press : root.button_press(8)


            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "9"
                on_press : root.button_press(9)


            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "x"
                on_press : root.math_sign("*")


            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "4"
                on_press : root.button_press(4)


            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "5"
                on_press : root.button_press(5)


            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "6"
                on_press : root.button_press(6)


            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "-"
                on_press : root.math_sign("-")


            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "1"
                on_press : root.button_press(1)


            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "2"
                on_press : root.button_press(2)


            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "3"
                on_press : root.button_press(3)


            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "+"
                on_press : root.math_sign("+")

#            Button:
 #               size_hint: (.2, .2)
  #              font_size: 32
   #             text: "+/-"

            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "0"
                on_press : root.button_press(0)


            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "."
                on_press : root.dot()

            Button:
                size_hint: (.2, .2)
                font_size: 32
                text: "="
                on_press :root.equals()

我在 vs 代码中没有遇到任何错误,即使在使用 google collab 制作 apk 时,但当我尝试在 andriod 上安装它时,它首先显示启动,但突然崩溃,同样发生在 bluestacks 上,如果你愿意,请告诉我了解任何额外的事情。

0 个答案:

没有答案
相关问题