Python Forum
[Tkinter] variable not defined
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] variable not defined
#1
My first post. Hope it done right.
Getting an error saying my variable not defined. I have tried several things and have watched at least 5 videos. Still no luck.

root = Tk()

root.title("Handifast Version 13.0")
root.geometry("1100x700")
root.iconbitmap("c:/guis/racehorse.ico")



#DEFINE setup COMMAND
def setup_command():
	global fyle_path
	top = Toplevel()
	top.title("Directory Path")
	top.geometry("300x150")
	my_label= Label(top, text= "Enter Race File Directory Path")
	my_label.pack()
	top.geometry("300x200")
	top.iconbitmap("c:/guis/racehorse.ico")
	e = Entry(top, font = ("sans_serif", 12))
	e.pack()
	fyle_path = e
	

my_label = Label(root, fyle_path)
my_label.pack()	
Error:
Traceback (most recent call last): File "Hello.py", line 28, in <module> my_label = Label(root, fyle_path) NameError: name 'fyle_path' is not defined
Thank You
Reply
#2
I don't know what you are trying to do with that label. I've never seen a tutorial that uses the same syntax as shown. Here's what they say about Label on effbot:

http://effbot.org/tkinterbook/label.htm

Label(master=None, **options)
import tkinter as tk

master = tk.Tk()

w = tk.Label(master, text="Hello, world!")
w.pack()

mainloop()
This is followed by a long list of the options, all of which use the key/value syntax like text in the above example.

But this is not the cause of your undefined variable. That problem is caused by, well, you not defining fyle_path. What is fyle_path supposed to be? Is it text that you want displayed in the label? Or is it the name of a file that contains an image you want to appear in the label? If the first, you need to use text=fyle_path and prior to that set fyle_path to some string, like fyle_path="Hello World".

If you are trying to display an image, that is done differently. First you have make an image, then set the image using image=myimage (or whatever variable you use). There are multiple ways to make an image. For example myimage= PhotoImage(file = fyle_path) where fyle_path is a str containing the name of the image file.

And though you see this from tkinter import * in a lot of examples, it is a really bad practice. If you use from import * a lot you will start having namespace collisions. A namespace collision is where you try to reference multiple things using the same name. When you do this in Python it silently keeps only the last referenc. When you call a function you may end up calling a function from the wrong module. Instead use import tkinter as tk and use the tk prefix when using tk resources
Reply
#3
I'm following a video course and this is what the instructor said to do. As for as what I'm trying to do, is take a value from Entry in the form of a string typed in by the user and write it to a text file. I'm switching over from a basic language and learning Python.

The last part of the code was to just do a test to see if the variable I wanted to write to file was actually whay I typed in the Entry widget.

Thank you for your help.
Reply
#4
Thanks for the help. Finally figured out what needed to be done. Here is the solution I came up with:

ef setup_command():
global fyle_path
global entry_1
fyle_path = StringVar()
entry_1 = StringVar()

I didn't realize the variables needed to be typed.


Milfredo
Reply
#5
StringVar() creates an instance of StringVar. fyle_path = StringVar() is not setting the type for fyle_path, It sets the variable "fyle_path" to reference the object returned by StringVar().

You should read about tkinter variables. They are not variables at all, but rather objects that are very useful when writing tkinger programs. If you don't understand how they work you are going to make a lot of mistakes and waste a lot of your time.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020