Python Forum

Full Version: GUI Calculator
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is a code that I've been working on for school. It's a calculator using tkinter. Can someone explain how I can fix this error? Also, what should I do in order for the buttons to function like buttons on a calculator using global variables. I'm not allowed to use lambda.

import tkinter
import tkinter.messagebox

from functools import partial 

class MyGUI:

    def button_of_power(self, number):
        self.answer_frame.configure(text = number)
    def __init__(self):
       
        self.main_window = tkinter.Tk()
        
       
        self.top_frame = tkinter.Frame(self.main_window)
        self.top_frame.pack()
        
        self.middle_frame = tkinter.Frame(self.main_window)
        self.middle_frame.pack()
        
        self.middle2_frame = tkinter.Frame(self.main_window)
        self.middle2_frame.pack()
        
        self.bottom_frame = tkinter.Frame(self.main_window)
        self.bottom_frame.pack()
        
        self.answer_frame = tkinter.Frame(self.main_window)
        self.answer_frame.pack()

        self.answer_space = tkinter.Label(self.answer_frame)
        self.answer_action = tkinter.Entry(self.answer_frame, width = 10)
        self.answer_action.pack()

       
        self.number_button1 = tkinter.Button(self.middle2_frame, text = '1', command = partial(self.button_of_power, '1'))
        self.number_button1.pack()
        self.number_button1.pack(side = 'left')
      
        
      
        self.number_button2 = tkinter.Button(self.middle2_frame, text = '2', command = partial(self.button_of_power, '2'))
        self.number_button2.pack()
        self.number_button2.pack(side = 'left')
  

       
        self.number_button3 = tkinter.Button(self.middle2_frame, text = '3', command = partial(self.button_of_power, '3'))
        self.number_button3.pack()
        self.number_button3.pack(side = 'left')
        
    
        self.number_button4 = tkinter.Button(self.middle2_frame, text = '-', command = partial(self.button_of_power, '-'))
        self.number_button4.pack()
        self.number_button4.pack(side = 'left')
  

        self.number_button5 = tkinter.Button(self.middle_frame, text = '4', command = partial(self.button_of_power, '4'))
        self.number_button5.pack()
        self.number_button5.pack(side = 'left')

        self.number_button6 = tkinter.Button(self.middle_frame, text = '5', command = partial(self.button_of_power, '5'))
        self.number_button6.pack()
        self.number_button6.pack(side = 'left')

        self.number_button7 = tkinter.Button(self.middle_frame, text = '6', command = partial(self.button_of_power, '6'))
        self.number_button7.pack()
        self.number_button7.pack(side = 'left')

        self.number_button8 = tkinter.Button(self.middle_frame, text = 'x', command = partial(self.button_of_power, 'x'))
        self.number_button8.pack()
        self.number_button8.pack(side = 'left')

        self.number_button9 = tkinter.Button(self.top_frame, text = '7', command = partial(self.button_of_power, '7'))
        self.number_button9.pack()
        self.number_button9.pack(side = 'left')

        self.number_button10 = tkinter.Button(self.top_frame, text = '8', command = partial(self.button_of_power, '8'))
        self.number_button10.pack()
        self.number_button10.pack(side = 'left')

        self.number_button11 = tkinter.Button(self.top_frame, text = '9', command = partial(self.button_of_power, '9'))
        self.number_button11.pack()
        self.number_button11.pack(side = 'left')

        self.number_button12 = tkinter.Button(self.top_frame, text = '÷', command = partial(self.button_of_power, '÷'))
        self.number_button12.pack()
        self.number_button12.pack(side = 'left')

        self.number_button13 = tkinter.Button(self.bottom_frame, text = '0', command = partial(self.button_of_power, '0'))
        self.number_button13.pack()
        self.number_button13.pack(side = 'left')

        self.number_button14 = tkinter.Button(self.bottom_frame, text = '.', command = partial(self.button_of_power, '.'))
        self.number_button14.pack()
        self.number_button14.pack(side = 'left')

        self.number_button15 = tkinter.Button(self.bottom_frame, text = '=', command = partial(self.button_of_power, '='))
        self.number_button15.pack()
        self.number_button15.pack(side = 'left')

        self.number_button16 = tkinter.Button(self.bottom_frame, text = '+', command = partial(self.button_of_power, '+'))
        self.number_button16.pack()
        self.number_button16.pack(side = 'left')

        

     

        

        

        

        tkinter.mainloop()

        

my_gui = MyGUI()



Error:
Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\yakam\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__ return self.func(*args) File "C:\Users\yakam\OneDrive\Documents\p2b_KaBe_proj02.py", line 11, in button_of_power self.answer_frame.configure(text = number) File "C:\Users\yakam\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1485, in configure return self._configure('configure', cnf, kw) File "C:\Users\yakam\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1476, in _configure self.tk.call(_flatten((self._w, cmd)) + self._options(cnf)) _tkinter.TclError: unknown option "-text"