1

I'm trying to make a GUI application of calculator using tkinter. I made a widget class that inherits from tkinter.Frame, in which I have a variable display_string(tkinter.StringVar) which contains the string to be displayed on the calculator string. I am not able to set any value to this variable by set(). Can someone please explain me what's wrong? I'm getting the following error on line 13.

TypeError: set() missing 1 required positional argument: 'value'

I followed this code example from the book "Python - GUI programming tkinter" from Alan D. Moore which worked fine.

Reference

import tkinter as tk
from tkinter import ttk

class HelloView(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)

        self.name = tk.StringVar()
        self.hello_string = tk.StringVar()
        #--------------------------------
        self.hello_string.set("Hello World")
        #-------------------------------------
        name_label = ttk.Label(self, text="Name: ")
        name_entry = ttk.Entry(self, textvariable=self.name)
        change_button = ttk.Button(self, text="Change", command=self.on_change)
        hello_label = ttk.Label(self, textvariable=self.hello_string,
                                font=("TkDefaultFont", 64), wraplength=600)
        # Layout form
        name_label.grid(row=0, column=0, sticky=tk.W)
        name_entry.grid(row=0, column=1, sticky=(tk.W + tk.E))
        change_button.grid(row=0, column=2, sticky=(tk.E))
        hello_label.grid(row=1, column=0, columnspan=3)
        self.columnconfigure(1, weight=1)

    def on_change(self):
        if self.name.get().strip():
            self.hello_string.set("Hello " + self.name.get())
        else:
            self.hello_string.set("Hello World")

class MyApplication(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # set the window properties
        self.title("Hello Tkinter")
        self.geometry("800x300")
        self.resizable(width=False, height=False)
        # define the ui
        HelloView(self).grid(sticky=(tk.E + tk.W + tk.N + tk.S))
        self.columnconfigure(0, weight=1)

if __name__ == '__main__':
    app = MyApplication()
    app.mainloop()

My Code

import tkinter as tk
from tkinter import ttk

class CalcView(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        super().__init__(parent, *args, **kwargs)

        self.display_string = tk.StringVar
        self.disp_ans = tk.IntVar
        self.operand1 = tk.IntVar
        self.operand2 = tk.IntVar
        self.operation = self.output

        # Here's the error:#---------------------------------------
        self.display_string.set("Hey")
        ##-----------------------------------------------------------

        display_label = ttk.Label(self, textvariable = self.display_string)

        button1 = ttk.Button(self, text = '7', command = self.add_seven)
        button2 = ttk.Button(self, text = '8', command = self.add_eight)
        button3 = ttk.Button(self, text = '9', command = self.add_nine)
        button4 = ttk.Button(self, text = '4', command = self.add_four)
        button5 = ttk.Button(self, text = '5', command = self.add_five)
        button6 = ttk.Button(self, text = '6', command = self.add_six)
        button7 = ttk.Button(self, text = '1', command = self.add_one)
        button8 = ttk.Button(self, text = '2', command = self.add_two)
        button9 = ttk.Button(self, text = '3', command = self.add_three)
        button10 = ttk.Button(self, text = '0', command = self.add_zero)

        button11 = ttk.Button(self, text = 'C', command = self.clear)
        button12 = ttk.Button(self, text = 'X', command = self.multiplication)
        button13 = ttk.Button(self, text = '\\', command = self.division)
        button14 = ttk.Button(self, text = '=', command = self.output)
        button15 = ttk.Button(self, text = '+', command = self.addition)
        button16 = ttk.Button(self, text = '-', command = self.subtraction)

        display_label.grid(row = 0, columnspan = 4, sticky = (tk.E + tk.W))

        button1.grid(row = 1, column = 0, sticky = (tk.E + tk.W))
        button2.grid(row = 1, column = 1, sticky = (tk.E + tk.W))
        button3.grid(row = 1, column = 2, sticky = (tk.E + tk.W))
        button4.grid(row = 2, column = 0, sticky = (tk.E + tk.W))
        button5.grid(row = 2, column = 1, sticky = (tk.E + tk.W))
        button6.grid(row = 2, column = 2, sticky = (tk.E + tk.W))
        button7.grid(row = 3, column = 0, sticky = (tk.E + tk.W))
        button8.grid(row = 3, column = 1, sticky = (tk.E + tk.W))
        button9.grid(row = 3, column = 2, sticky = (tk.E + tk.W))
        button10.grid(row = 4, column = 0, sticky = (tk.E + tk.W))
        button11.grid(row = 1, column = 3, sticky = (tk.E + tk.W))
        button12.grid(row = 2, column = 3, sticky = (tk.E + tk.W))
        button13.grid(row = 3, column = 3, sticky = (tk.E + tk.W))
        button14.grid(row = 4, column = 3, sticky = (tk.E + tk.W))
        button15.grid(row = 4, column = 2, sticky = (tk.E + tk.W))
        button16.grid(row = 4, column = 1, sticky = (tk.E + tk.W))

        self.columnconfigure(0, weight = 1)

    def add_nine(self):
        pass#self.display_string.set(self.display_string.get() + '9')

    def add_eight(self):
        pass#self.display_string.set(self.display_string.get() + '8')

    def add_seven(self):
        pass#self.display_string.set(self.display_string.get() + '7')

    def add_six(self):
        pass#self.display_string.set(self.display_string.get() + '6')

    def add_five(self):
        pass#self.display_string.set(self.display_string.get() + '5')

    def add_four(self):
        pass#self.display_string.set(self.display_string.get() + '4')

    def add_three(self):
        pass#self.display_string.set(self.display_string.get() + '3')

    def add_two(self):
        pass#self.display_string.set(self.display_string.get() + '2')

    def add_one(self):
        pass#self.display_string.set(self.display_string.get() + '1')

    def add_zero(self):
        pass#self.display_string.set(self.display_string.get() + '0')

    def clear(self):
        pass

    def multiplication(self):
        pass

    def division(self):
        pass

    def addition(self):
        pass

    def subtraction(self):
        pass

    def output(self):
        pass

class MainApplication(tk.Tk):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.title("Calculator")
        self.geometry("300x600")
        self.resizable(width = False, height = False)

        CalcView(self).grid(sticky = (tk.N + tk.S + tk.E + tk.W))

if __name__ == "__main__":
    app = MainApplication()
    app.mainloop()
1
  • 2
    Please try to reduce this code down to a minimal reproducible example. If the problem is with calling one method on one object, we don't need 16 buttons and a bunch of functions that are completely unrelated to the problem. Commented Jan 24, 2019 at 13:31

1 Answer 1

2

I got the fix. I did not initialise the tkinter.StrinVar() properly. I should have typed tk.StringVar() instead of tk.StringVar. Same with the next three lines too. My bad. :)

Sign up to request clarification or add additional context in comments.

2 Comments

Good job, took me a few minutes to catch it.. was about to submit that same answer!
I was about to test the code to see whats up and I didn't even see the missing parenthesis. Just goes to show sometimes the simple things can allude us.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.