from tkinter import * class Main(Frame): def init(self, root): super(Main,…
- User
from tkinter import *
class Main(Frame): def init(self, root): super(Main, self).init(root) self.build()
def build(self): pass def logicalc(self, operation): pass def update(): passif name == 'main': root = Tk() root["bg"] = "#000" root.geometry("485x550+200+200") root.title("Калькулятор") root.resizable(False, False) app = Main(root) app.pack() root.mainloop()
- Assistant
The code provided is a basic template for a calculator using Tkinter. The Main class inherits from the Frame class and includes methods for building the GUI, performing logical calculations, and updating the display. The code also includes the main part where the Tkinter root window is created and the Main class is instantiated.
- User
код не работает,почини
- Assistant
Извините за предоставленный неработающий код. Вот исправленная версия:
from tkinter import * class Main(Frame): def __init__(self, root): super(Main, self).__init__(root) self.root = root self.root["bg"] = "#000" self.root.geometry("485x550+200+200") self.root.title("Калькулятор") self.root.resizable(False, False) self.build() def build(self): self.entry = Entry(self.root, font=("Arial", 20), bd=5, justify=RIGHT) self.entry.place(x=10, y=10, width=465, height=70) btns = [ '7', '8', '9', '/', '4', '5', '6', '*', '1', '2', '3', '-', '0', '.', '=', '+' ] x = 10 y = 90 for btn in btns: Button( self.root, text=btn, font=("Arial", 15), bd=5, command=lambda btn=btn: self.logicalc(btn) ).place(x=x, y=y, width=110, height=80) x += 115 if x > 400: x = 10 y += 85 def logicalc(self, operation): if operation == '=': try: result = eval(self.entry.get()) self.entry.delete(0, END) self.entry.insert(END, str(result)) except: self.entry.delete(0, END) self.entry.insert(END, 'Ошибка') else: self.entry.insert(END, operation) def update(self): pass if __name__ == '__main__': root = Tk() app = Main(root) app.pack() root.mainloop()В этой версии кода добавлены методы для создания кнопок и обработки логических операций. Теперь код должен работать должным образом и отображать окно калькулятора с кнопками и полем ввода.