Home > Enterprise >  Canvas not appearing and its saying tkinter is not defined (tkinter python)
Canvas not appearing and its saying tkinter is not defined (tkinter python)

Time:10-01

I'm new to programming and the book I'm using gives the below code. The canvas is not appearing when i run the program and the console is giving me an error message 'unable to detect unidentified names' and '(insert variable) may be unidentified or identified by star imports' -- my book is saying its part of the standard library can anyone help explain what I'm doing wrong?

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from tkinter import *
def hello():
    print("hello buddy")
tk = Tk()
btn = Button(tk, text="click me!", command=hello)
btn.pack()
canvas = tkinter.Canvas(tk, width=500, height=500)
canvas.pack()

CodePudding user response:

since you imported tkinter with the * sign, you do not need to add tkinter before a tkinter method/class call. Everything in tkinter is loaded to the program. So simply remove the tkinter. from the canvas line and it should work.

CodePudding user response:

Change this.

canvas = tkinter.Canvas(tk, width=500, height=500)

to:

canvas = Canvas(tk, width=500, height=500)

Output:

enter image description here

CodePudding user response:

tKinter is not part of the standard library. You can install it by running:

pip install tk

It is included with some installations of Python, but not all.

CodePudding user response:

Make sure that pip is installed on your computer/laptop.

Then, open cmd/terminal in your operating system and type:

pip install tk
  • Related