用Kivy写一个能在手机上运行的记账软件
时间: 2023-06-18 09:02:36 浏览: 192
好的,以下是一个简单的记账软件的代码示例,使用 Python 和 Kivy 编写:
```python
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
class ExpenseTracker(BoxLayout):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.expense_label = Label(text="Expense:")
self.expense_input = TextInput(multiline=False)
self.amount_label = Label(text="Amount:")
self.amount_input = TextInput(multiline=False)
self.submit_button = Button(text="Submit", on_press=self.add_expense)
self.expense_list = BoxLayout(orientation="vertical")
self.add_widget(self.expense_label)
self.add_widget(self.expense_input)
self.add_widget(self.amount_label)
self.add_widget(self.amount_input)
self.add_widget(self.submit_button)
self.add_widget(self.expense_list)
def add_expense(self, instance):
expense = self.expense_input.text
amount = self.amount_input.text
self.expense_list.add_widget(Label(text=f"{expense}: {amount}"))
class ExpenseTrackerApp(App):
def build(self):
return ExpenseTracker()
if __name__ == "__main__":
ExpenseTrackerApp().run()
```
这个应用程序有一个文本输入框,用于输入支出的名称和金额,以及一个提交按钮,用于将支出添加到支出列表中。支出列表是一个垂直的 BoxLayout,其中包含每个支出的名称和金额。你可以根据需要进行修改和扩展此示例代码以满足你的需求,例如添加日期选择器、支出类别选择等等。
阅读全文