-2

I am making a basic temperture converter, I have it so you can convert celcius to farenheight, and now im trying to make it so you can switch. I have this code:

from tkinter import *
bool1 = True
class App:
    def __init__(self, master):
        frame = Frame(master)
        frame.pack()
        self.x = Label(frame, text = 'Celcius:').grid(row = 0, column = 0)
        self.c_var = DoubleVar()
        Entry(frame, textvariable = self.c_var).grid(row = 0, column = 1)
        self.z = Label(frame, text = 'Farenheight:').grid(row = 1, column = 0)
        self.result_var = DoubleVar()
        Label(frame, textvariable = self.result_var).grid(row = 1, column = 1)
        b1 = Button(frame, text = 'Switch', command = self.switch)
        b1.grid(row = 2, columnspan = 2)
        button = Button(frame, text = 'Convert', command = self.convert)
        button.grid(row = 3, columnspan = 2)
        return None
    def convert(self):
        c = self.c_var.get()
        c = c * (9/5) + 32
        self.result_var.set(c)
    def switch(self):
        global bool1
        if bool1 == True:
            bool1 = False
            self.x.config(text = 'Farenheight:')
        else:
            bool1 = True
            self.z['text'] = 'Celcius:'
root = Tk()
root.wm_title('Temp Converter')
app = App(root)
root.mainloop()

The error message I am getting is:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\keith\AppData\Local\Programs\Python\Python35-    32\lib\tkinter\__init__.py", line 1550, in __call__
    return self.func(*args)
  File "C:\Users\keith\Desktop\tkinter.py", line 26, in switch
    self.x.config(text = 'Farenheight:')
AttributeError: 'NoneType' object has no attribute 'config'

1 Answers1

0

The problem comes from the fact that you assigned the x and z attributes (self.x, self.z) not to the tkinter labels but to whatever the Tkinter.Label.grid() function returns, which is None.

Instead, separate the declaration of the label and their grid configuration onto two lines that first declares the variables and then calls the grid function on them, thereby assigning x and z to the labels themselves.

from tkinter import *
bool1 = True
class App:
    def __init__(self, master):
        frame = Frame(master)
        frame.pack()
        self.x = Label(frame, text = 'Celcius:')
        self.x.grid(row = 0, column = 0)
        self.c_var = DoubleVar()
        Entry(frame, textvariable = self.c_var).grid(row = 0, column = 1)
        self.z = Label(frame, text = 'Farenheight:')
        self.z.grid(row = 1, column = 0)
        self.result_var = DoubleVar()
        Label(frame, textvariable = self.result_var).grid(row = 1, column = 1)
        b1 = Button(frame, text = 'Switch', command = self.switch)
        b1.grid(row = 2, columnspan = 2)
        button = Button(frame, text = 'Convert', command = self.convert)
        button.grid(row = 3, columnspan = 2)
        return None
    def convert(self):
        c = self.c_var.get()
        c = c * (9/5) + 32
        self.result_var.set(c)
    def switch(self):
        global bool1
        if bool1 == True:
            bool1 = False
            self.x.config(text = 'Farenheight:')
        else:
            bool1 = True
            self.z['text'] = 'Celcius:'
root = Tk()
root.wm_title('Temp Converter')
app = App(root)
root.mainloop()
Jonas
  • 1,473
  • 2
  • 13
  • 28