[英]Simple calculator issue in Kivy
I'm working on a calculator which calculates depending on how much you are spending, how much money you'll have at the end of the month.我正在开发一个计算器,它会根据您的支出和月底的收入进行计算。
I'll provide you with both the .kv and .py.我将为您提供 .kv 和 .py。 The thing is, I've done the visuals and I've created the calculator class which contains the math, now I need to combine it into a functioning GUI app.
问题是,我已经完成了视觉效果并创建了包含数学的计算器类,现在我需要将它组合成一个正常运行的 GUI 应用程序。
So the thing I want to achieve is that the app, when you click on the button labeled "izracunaj", the Calculator class ( in ''' ) which should take the user input and calculate it and return a result.所以我想要实现的是应用程序,当你点击标记为“izracunaj”的按钮时,计算器类(在 ''' 中)应该接受用户输入并计算它并返回结果。
main.py主文件
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
class Screen(GridLayout):
def __init__(self, **kwargs):
super(Screen, self).__init__(**kwargs)
self.cols = 1
self.add_widget(Label(text="Koliko imas na racunu"))
self.stanjeNovaca = TextInput(multiline=False)
self.add_widget(self.stanjeNovaca)
self.add_widget(Label(text="Koliko dana do place"))
self.stanjeDani = TextInput(multiline=False)
self.add_widget(self.stanjeDani)
self.add_widget(Label(text="Koliko trosis na dan"))
self.stanjePotrosnja = TextInput(multiline=False)
self.add_widget(self.stanjePotrosnja)
'''
class Calculator():
def calculation():
stanjeNovaca = input("Koliko imas para na racunu? ")
stanjeDani = input("Koliko dana do place? ")
stanjePotrosnja = input("koliko trosis na dan? ")
svakiDanTrosis = stanjeDani*stanjePotrosnja
naKrajuMjeseca = stanjeNovaca-svakiDanTrosis
print("Ako svaki dan trosis {}, na kraju mjeseca ce ti ostati {}").format(stanjePotrosnja, naKrajuMjeseca)
return calculation();
'''
class CalculatorApp(App):
def build(self):
return Screen()
CalculatorApp().run()
kv千伏
<Screen>
Button:
text: "izracunaj"
I fixed your program. 我已修复您的程序。 The added command in the kv-file triggers the calculations when you press the button and the result will be printed.
当您按下按钮时,kv文件中添加的命令将触发计算,结果将被打印出来。
main.py main.py
import kivy
from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.image import Image
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.textinput import TextInput
class Screen(GridLayout):
def __init__(self, **kwargs):
super(Screen, self).__init__(**kwargs)
self.cols = 1
self.add_widget(Label(text="Koliko imas na racunu"))
self.stanjeNovaca = TextInput(multiline=False)
self.add_widget(self.stanjeNovaca)
self.add_widget(Label(text="Koliko dana do place"))
self.stanjeDani = TextInput(multiline=False)
self.add_widget(self.stanjeDani)
self.add_widget(Label(text="Koliko trosis na dan"))
self.stanjePotrosnja = TextInput(multiline=False)
self.add_widget(self.stanjePotrosnja)
def calculation(self):
svakiDanTrosis = float(self.stanjeDani.text) * float(self.stanjePotrosnja.text)
naKrajuMjeseca = float(self.stanjeNovaca.text) - svakiDanTrosis
print("Ako svaki dan trosis {}, na kraju mjeseca ce ti ostati {}".format(self.stanjePotrosnja.text, naKrajuMjeseca))
return naKrajuMjeseca
class CalculatorApp(App):
def build(self):
return Screen()
CalculatorApp().run()
calculator.kv 计算器
<Screen>
Button:
text: "izracunaj"
on_release: root.calculation()
If you want to show the result in the GUI, just add a Widget
(eg a Label
) to your Screen
and set its text accordingly. 如果要在GUI中显示结果,只需在
Screen
添加Widget
(例如Label
)并相应地设置其文本即可。
This calculator looks like Windows 10 calculator in dark mode这个计算器在黑暗模式下看起来像 Windows 10 计算器
main.py(down) main.py(下)
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.lang.builder import Builder
import re
Builder.load_file('./calculator.kv')
Window.size = (350, 550)
class CalculatorWidget(Widget):
# Clear the screen
def clear(self):
self.ids.input_box.text = "0"
# Remove the last character
def remove_last(self):
prev_number = self.ids.input_box.text
prev_number = prev_number[:-1]
self.ids.input_box.text = prev_number
# Getting the button value
def button_value(self, number):
prev_number = self.ids.input_box.text
if "wrong equation" in prev_number:
prev_number = ''
if prev_number == '0':
self.ids.input_box.text = ''
self.ids.input_box.text = f"{number}"
else:
self.ids.input_box.text = f"{prev_number}{number}"
# Getting the sings
def sings(self, sing):
prev_number = self.ids.input_box.text
self.ids.input_box.text = f"{prev_number}{sing}"
# Getting decimal value
def dot(self):
prev_number = self.ids.input_box.text
num_list = re.split("\+|\*|-|/|%", prev_number)
if ("+" in prev_number or "-" in prev_number or "*" in prev_number or "/" in prev_number or "%" in prev_number) and "." not in num_list[-1]:
prev_number = f"{prev_number}."
self.ids.input_box.text = prev_number
elif '.' in prev_number:
pass
else:
prev_number = f'{prev_number}.'
self.ids.input_box.text = prev_number
# Calculate the result
def results(self):
prev_number = self.ids.input_box.text
try:
result = eval(prev_number)
self.ids.input_box.text = str(result)
except:
self.ids.input_box.text = "wrong equation"
# Positive to negative
def positive_negative(self):
prev_number = self.ids.input_box.text
if "-" in prev_number:
self.ids.input_box.text = f"{prev_number.replace('-', '')}"
else:
self.ids.input_box.text = f"-{prev_number}"
class CalculatorApp(App):
def build(self):
return CalculatorWidget()
if __name__ == "__main__":
CalculatorApp().run()
And below code in the .kv file下面是 .kv 文件中的代码
<Button>
size_hint: (0.2, 0.2)
background_normal: ""
background_color: (0, 0, 0, 0)
<CalculatorWidget>
BoxLayout
orientation: "vertical"
size: root.width, root.height
TextInput:
id: input_box
text: "0"
font_size: 43
multiline: False
halign: "right"
size_hint: (1, 0.19)
background_color: (20/255, 20/255, 30/255, 1)
foreground_color: (1, 1, 1, 1)
GridLayout:
cols: 4
rows: 5
### Row 1 ####
Button:
text: "%"
font_size: 32
on_press: root.sings("%")
background_color: (14/255, 73/255, 176/255, 1)
Button:
text: "C"
font_size: 32
on_press: root.clear()
background_color: (14/255, 73/255, 176/255, 1)
Button:
text: u"\u00AB"
font_size: 32
on_press: root.remove_last()
background_color: (14/255, 73/255, 176/255, 1)
Button:
text: "/"
font_size: 32
background_color: (14/255, 73/255, 176/255, 1)
on_press: root.sings("/")
background_color: (14/255, 73/255, 176/255, 1)
#### Row 2 #####
Button:
text: "7"
font_size: 32
on_press: root.button_value(7)
Button:
text: "8"
font_size: 32
on_press: root.button_value(8)
Button:
text: "9"
font_size: 32
on_press: root.button_value(9)
Button:
text: "x"
font_size: 32
background_color: (14/255, 73/255, 176/255, 1)
on_press: root.sings("*")
#### Row 3 ###
Button:
text: "4"
font_size: 32
on_press: root.button_value(4)
Button:
text: "5"
font_size: 32
on_press: root.button_value(5)
Button:
text: "6"
font_size: 32
on_press: root.button_value(6)
Button:
text: "-"
font_size: 32
background_color: (14/255, 73/255, 176/255, 1)
on_press: root.sings("-")
### Row 4 ###
Button:
text: "1"
font_size: 32
on_press: root.button_value(1)
Button:
text: "2"
font_size: 32
on_press: root.button_value(2)
Button:
text: "3"
font_size: 32
on_press: root.button_value(3)
Button:
text: "+"
font_size: 32
background_color: (14/255, 73/255, 176/255, 1)
on_press: root.sings("+")
### Row 5 ###
Button:
text: "+/-"
font_size: 32
on_press: root.positive_negative()
Button:
text: "0"
font_size: 32
on_press: root.button_value(0)
Button:
text: "."
font_size: 32
on_press: root.dot()
Button:
text: "="
font_size: 32
background_color: (14/255, 73/255, 176/255, 1)
on_press: root.results()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.