Я пытаюсь научить себя Python Киви, и я работал с этой программой калькулятора. Я хочу сделать текст для показателя степени отличным от того, что обрабатывается. Обычно это **, но я хочу, чтобы он показывал ^ на кнопке. Как получить, чтобы показать одну вещь и вставить другую в элементе entry.text + = self.text. Другой способ, которым я думал, пытался использовать декоратор, но я не уверен, как это сделать для такого оператора.
Есть идеи?
main.py
import kivy
from kivy.app import App
kivy.require('1.11.1')
from kivy.uix.gridlayout import GridLayout
from kivy.config import Config
Config.set('graphics','resizable',1)
class CalcGridLayout(GridLayout):
def calculate(self,calculation):
if calculation:
try:
self.display.text = str(eval(calculation))
except Exception:
self.display.text = "Error"
class CalculatorApp(App):
def build(self):
return CalcGridLayout()
calcApp = CalculatorApp()
calcApp.run()
calculator.kv
# Custom button
<CustButton@Button>:
font_size: 32
# Define id so I can refer to the CalcGridLayout
# class functions
# Display points to the entry widget
<CalcGridLayout>:
id: calculator
display: entry
rows: 6
padding: 10
spacing: 10
# Where input is displayed
BoxLayout:
TextInput:
id: entry
font_size: 32
multiline: False
# When buttons are pressed update the entry
BoxLayout:
spacing: 5
CustButton:
text: "+"
on_press: entry.text += self.text
CustButton:
text: "-"
on_press: entry.text += self.text
CustButton:
text: "*"
on_press: entry.text += self.text
CustButton:
text: "/"
on_press: entry.text += self.text
CustButton:
text: "("
on_press: entry.text += self.text
CustButton:
text: ")"
on_press: entry.text += self.text
CustButton:
text: "**"
on_press: entry.text += self.text
BoxLayout:
spacing: 5
CustButton:
text: "1"
on_press: entry.text += self.text
CustButton:
text: "2"
on_press: entry.text += self.text
CustButton:
text: "3"
on_press: entry.text += self.text
CustButton:
text: "4"
on_press: entry.text += self.text
CustButton:
text: "5"
on_press: entry.text += self.text
CustButton:
text: "AC"
on_press: entry.text = ""
BoxLayout:
spacing: 5
CustButton:
text: "6"
on_press: entry.text += self.text
CustButton:
text: "7"
on_press: entry.text += self.text
CustButton:
text: "8"
on_press: entry.text += self.text
CustButton:
text: "9"
on_press: entry.text += self.text
CustButton:
text: "0"
on_press: entry.text += self.text
CustButton:
text: "="
on_press: calculator.calculate(entry.text)
```