Я новичок в tkinter и пытаюсь создать калькулятор, но я не могу передать значение из одного класса в другой? - PullRequest
0 голосов
/ 13 марта 2020

Я нуб, просто помогите мне, пожалуйста Было бы очень полезно, если бы кто-нибудь помог мне создать этот проект, помогая мне шаг за шагом и предлагая вещи, которые я могу сделать лучше ... это просто не работает и я не знаю почему?

import tkinter as tk
from tkinter import ttk
try:
    from ctypes import windll
    windll.shcore.SetDpiAwareness(1)
except Exception as e:
    print(e)
    pass

class App(tk.Tk):
    def __init__(self):
        super().__init__()

        self.title('Calculator')
        self.resizable(False, False)
        self.geometry('600x350')

        display_frame = Display(self)
        display_frame.grid(row=0, column=0, sticky='NSEW')

        button_frame = Buttons(self)
        button_frame.grid(row=1, column=0, sticky='NSEW')

class Display(ttk.Frame):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)

        self.input_value = tk.StringVar()

        display_entry = ttk.Entry(self, width=91, justify='center', textvariable=self.input_value)
        display_entry.grid(padx=15, pady=15, ipadx=10, ipady=10, row=0, column=0, columnspan=2, sticky='E')
        display_entry.focus()

class Buttons(ttk.Frame):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)

        expression = ''

        def press(num):
            # point out the global expression variable
            global expression

            # concatenation of string
            expression = expression + str(num)

            # update the expression by using set method
            self.input_value.set(expression)

            # Function to evaluate the final expression

        def equalpress():
            # Try and except statement is used
            # for handling the errors like zero
            # division error etc.

            # Put that code inside the try block
            # which may generate the error
            try:

                global expression

                # eval function evaluate the expression
                # and str function convert the result
                # into string
                total = str(eval(expression))

                self.input_value.set(total)

                # initialize the expression variable
                # by empty string
                expression = ""

                # if error is generate then handle
            # by the except block
            except:

                self.input_value.set(" error ")
                expression = ""

                # Function to clear the contents

        # of text entry box
        def clear():
            global expression
            expression = ""
            self.input_value.set("")

        button_1 = ttk.Button(self, text='1', command=lambda: press('1'))
        button_1.grid(padx=10, pady=10, ipadx=10, ipady=10, row=0, column=0, sticky='NSEW')

        button_2 = ttk.Button(self, text='2', command=lambda: press('2'))
        button_2.grid(padx=10, pady=10, ipadx=10, ipady=10, row=0, column=1, sticky='NSEW')

        button_3 = ttk.Button(self, text='3', command=lambda: press('3'))
        button_3.grid(padx=10, pady=10, ipadx=10, ipady=10, row=0, column=2, sticky='NSEW')

        button_4 = ttk.Button(self, text='4', command=lambda: press('4'))
        button_4.grid(padx=10, pady=10, ipadx=10, ipady=10, row=1, column=0, sticky='NSEW')

        button_5 = ttk.Button(self, text='5', command=lambda: press('5'))
        button_5.grid(padx=10, pady=10, ipadx=10, ipady=10, row=1, column=1, sticky='NSEW')

        button_6 = ttk.Button(self, text='6', command=lambda: press('6'))
        button_6.grid(padx=10, pady=10, ipadx=10, ipady=10, row=1, column=2, sticky='NSEW')

        button_7 = ttk.Button(self, text='7', command=lambda: press('7'))
        button_7.grid(padx=10, pady=10, ipadx=10, ipady=10, row=2, column=0, sticky='NSEW')

        button_8 = ttk.Button(self, text='8', command=lambda: press('8'))
        button_8.grid(padx=10, pady=10, ipadx=10, ipady=10, row=2, column=1, sticky='NSEW')

        button_9 = ttk.Button(self, text='9', command=lambda: press('9'))
        button_9.grid(padx=10, pady=10, ipadx=10, ipady=10, row=2, column=2, sticky='NSEW')

        button_0 = ttk.Button(self, text='0', command=lambda: press('0'))
        button_0.grid(padx=10, pady=10, ipadx=10, ipady=10, row=3, column=0, sticky='NSEW')

        button_clear = ttk.Button(self, text='C', command=lambda: press('1'))
        button_clear.grid(padx=10, pady=10, ipadx=10, ipady=10, row=0, column=3, sticky='NSEW')

        button_clear_all = ttk.Button(self, text='CE', command=lambda: clear)
        button_clear_all.grid(padx=10, pady=10, ipadx=10, ipady=10, row=0, column=4, sticky='NSEW')

        button_add = ttk.Button(self, text='+', command=lambda: press('+'))
        button_add.grid(padx=10, pady=10, ipadx=10, ipady=10, row=1, column=3, sticky='NSEW')

        button_sub = ttk.Button(self, text='-', command=lambda: press('-'))
        button_sub.grid(padx=10, pady=10, ipadx=10, ipady=10, row=1, column=4, sticky='NSEW')

        button_mul = ttk.Button(self, text='*', command=lambda: press('*'))
        button_mul.grid(padx=10, pady=10, ipadx=10, ipady=10, row=2, column=3, sticky='NSEW')

        button_div = ttk.Button(self, text='÷', command=lambda: press('/'))
        button_div.grid(padx=10, pady=10, ipadx=10, ipady=10, row=2, column=4, sticky='NSEW')

        button_fact = ttk.Button(self, text='!')
        button_fact.grid(padx=10, pady=10, ipadx=10, ipady=10, row=3, column=3, sticky='NSEW')

        button_square_root = ttk.Button(self, text='√')
        button_square_root.grid(padx=10, pady=10, ipadx=10, ipady=10, row=3, column=4, sticky='NSEW')

        button_equal = ttk.Button(self, text='=', command=equalpress)
        button_equal.grid(padx=10, pady=10, ipadx=10, ipady=10, row=3, column=1, columnspan=2, sticky='NSEW')

    if __name__ == '__main__':
    app = App()
    app.mainloop()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...