kalintri
kalintri

Reputation: 21

Trying to write Python tool using tkinter, but the window comes up?

I am trying to build a simple sum calculator, but the tkinter window comes up empty without any errors. I am running it in PyCharm on the latest MacOS- 10.14 and with the latest Python- 3.7.

I can't really figure out what am I missing. I am sure it is something obvious, but I guess, I just need a fresh pair of eyes. Any help will be much appreciated.

Here is the code:

import tkinter as tk


class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)

        self.pack()


# create the application
app = Application()
app.master.title("Sumator")
app.master.minsize(width=1000, height=500)

# start the program
app.mainloop()


def __init__(self, master=None):
    super().__init__(master)

    self.pack()
    self.create_widgets()


def create_widgets(self):
    # create widgets
    self.firstNumberEntry = tk.Entry()
    self.plusSign = tk.Label(text="+")
    self.secondNumberEntry = tk.Entry()
    self.equalSign = tk.Label(text="=")
    self.resultLabel = tk.Label(text="Result...", bg="green", fg="white")
    self.calculateButton = tk.Button(text="Calculate", command=self.calculate)

    # place widgets
    self.firstNumberEntry.pack(side="left")
    self.plusSign.pack(side="left")
    self.secondNumberEntry.pack(side="left")
    self.equalSign.pack(side="left")
    self.resultLabel.pack(side="left")
    self.calculateButton.pack(side="left")


def calculate(self):
    first_value = float(self.firstNumberEntry.get())
    second_value = float(self.secondNumberEntry.get())
    result = first_value + second_value
    self.resultLabel.config(text=str(result), bg="green", fg="white")

Upvotes: 0

Views: 80

Answers (1)

Bitto
Bitto

Reputation: 8225

Your class methods are defined outside the class and you have def __init__(self, master=None) twice.

import tkinter as tk


class Application(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()
        self.create_widgets()


    def create_widgets(self):
        # create widgets
        self.firstNumberEntry = tk.Entry()
        self.plusSign = tk.Label(text="+")
        self.secondNumberEntry = tk.Entry()
        self.equalSign = tk.Label(text="=")
        self.resultLabel = tk.Label(text="Result...", bg="green", fg="white")
        self.calculateButton = tk.Button(text="Calculate", command=self.calculate)

        # place widgets
        self.firstNumberEntry.pack(side="left")
        self.plusSign.pack(side="left")
        self.secondNumberEntry.pack(side="left")
        self.equalSign.pack(side="left")
        self.resultLabel.pack(side="left")
        self.calculateButton.pack(side="left")


    def calculate(self):
        first_value = float(self.firstNumberEntry.get())
        second_value = float(self.secondNumberEntry.get())
        result = first_value + second_value
        self.resultLabel.config(text=str(result), bg="green", fg="white")

# create the application
app = Application()
app.master.title("Sumator")
app.master.minsize(width=1000, height=500)

# start the program
app.mainloop()

Upvotes: 1

Related Questions