Форум сайта python.su
С помощью среза удалить не получается
import kivy kivy.require('1.0.7') from kivy.app import App from kivy.uix.boxlayout import BoxLayout from kivy.uix.textinput import TextInput from kivy.uix.button import Button class MainApp(App): def build(self): main_layout = BoxLayout(orientation="vertical", padding=10, spacing=10) self.solution = TextInput(multiline=False, readonly=False, halign="right", font_size=55, input_filter="float") main_layout.add_widget(self.solution) buttons = [ ["7", "8", "9", "/"], ["4", "5", "6", "*"], ["1", "2", "3", "-"], [".", "0", "C", "+"], ] for row in buttons: h_layout = BoxLayout() for label in row: button = Button(text=label, pos_hint={"center_x": 0.5, "center_y": 0.5}, font_size=55) button.bind(on_press=self.on_button_press) h_layout.add_widget(button) main_layout.add_widget(h_layout) equals_button = Button(text="=", pos_hint={"center_x": 0.5, "center_y": 0.5}, font_size=55) equals_button.bind(on_press=self.on_solution) main_layout.add_widget(equals_button) del_button = Button(text="Del", pos_hint={"center_x": 0.5, "center_y": 0.5}, font_size=55) del_button.bind(on_press=self.on_solution) main_layout.add_widget(del_button) return main_layout def on_button_press(self, instance): if instance.text == "C": self.solution.text = "" elif instance.text == "Del": # проверка на нажатие Del if self.solution.text: # Проверяем, что строка не пуста self.solution.text = self.solution.text[:-1] else: self.solution.text += instance.text def on_solution(self, instance): if self.solution.text: try: self.solution.text = str(eval(self.solution.text)) except: self.solution.text = "Error" if __name__ == '__main__': MainApp().run()
Отредактировано Logran (Фев. 17, 2024 16:27:17)
Офлайн
Вопрос закрыт, разобрался.
Офлайн