top of page

Dark Themed Tkinter Python Program Adds Two Numbers NameError: name 'tkinter' is not defined Solved

  • Writer: Brian Clark
    Brian Clark
  • Apr 14, 2022
  • 1 min read

Updated: Apr 21, 2022

Today, I'm still working on the code from yesterday, which added two numbers in Tkinter. I solved the problem of placing the result in the third entry window. On day two of working on this simple program, I wanted to change the look and feel of the program.


I ran into this simple error today:

NameError: name 'tkinter' is not defined

I found the solution using the following source:


Shakad M. (Jun 3, 2020) NameError: name 'tkinter' is not defined. Stack Overflow.https://stackoverflow.com/questions/62170613/nameerror-name-tkinter-is-not-defined


After doing some hunting, I found an additional source to stylize my simple tkinter program here:


Tom S. (April 4, 2022) CustomTkinter UI-Library. Github


So after I figured out what I needed to fix, I produced the following code which looks much better than yesterday's program.



Author: Brian Clark 
Website : https://brianclark88.wixsite.com/mysite

import tkinter

import customtkinter

window= tkinter.Tk()

customtkinter.set_appearance_mode("dark")  # Modes: system (default), light, dark
customtkinter.set_default_color_theme("blue")  # Themes: blue (default), dark-blue, green

def addNumbers():
    res = int(e1.get())+int(e2.get())
    myText.set(res)
    e3.insert(3, res)


master = customtkinter.CTk()
  # create CTk window like you do with the Tk window
master.geometry("400x240")


myText = tkinter.StringVar()
tkinter.Label(master, text="First").grid(row=0, sticky=tkinter.W)
tkinter.Label(master, text="Second").grid(row=1, sticky=tkinter.W)
tkinter.Label(master, text="Result:").grid(row=3, sticky=tkinter.W)
result = tkinter.Label(master, text="", textvariable=myText).grid(
    row=3, column=1, sticky=tkinter.W)

e1 = tkinter.Entry(master)
e2 = tkinter.Entry(master)
e3 = tkinter.Entry(master)

e1.grid(row=0, column=1)
e2.grid(row=1, column=1)
e3.grid(row=3, column=1)

b = customtkinter.CTkButton(master, text="Calculate", command=addNumbers)
b.grid(row=0, column=0, sticky=tkinter.E)



window.mainloop()


Here is how the program looks:


Simply place two numbers in the first two spaces and click the "=" sign. I'm going to turn this program into a full-fledged program as I finish the tutorials I'm studying.


Thanks for stopping by.




Comments


bottom of page