Tkinter fails to pass Entry data between two functions - python

When I run the following code, I receive the following error: NameError: name 'barE' is not defined.
Does Tkinter's Entry work from one function to another? I have a global window and frame in this program and it passes the user's input without error using the same syntax as below.
def newSearchForm():
window2=Tk()
window2.geometry("650x400")
frame = Frame(window2)
frame.pack(side="top", expand=True, fill="both")
barcode=Label(frame,text="Please Enter The Barcode", font='time 15')
barcode.place(x=10,y=90)
barE=Entry(frame)
barE.place(x=250,y=90)
isbn=Label(frame,text="Please Enter The ISBN", font='time 15')
isbn.place(x=10,y=130)
isbE=Entry(frame)
isbE.place(x=250,y=130)
repeatSearchButton=Button(frame,text="Enter", command=newSearch,width=12,bg='gray')
repeatSearchButton.place(x=150,y=170)
window.mainloop()
def newSearch():
uB = barE.get()
uI = isbE.get()
carlSearch(uB, uI)
itemTitle=workspace.find_element_by_xpath('//*[#id="mainContent"]/div[2]/div[2]/div[2]/div[1]/div[1]/div').text
ingramSearch()
fantasticFictionSearch(itemTitle)
outputMetadata()
I tried using the lambda command to explicitly pass the variables and that didn't work.
I tried using anexising window and that didn't work, so I destroyed it and created a new one.

I would suggest you to use text variable. You can create a text variable by StringVar and assign it in the entry widget.
For more details on why to use text variable see Should I use Entry's .get() or its textvariable's for Tkinter in Python?
Usage:
barE_var=StringVar()
barE=Entry(frame,textvariable=barE_var)
barE.place(x=250,y=90)
To get value use barE_var.get().
Does Tkinter's Entry work from one function to another?
It does work, but have to use global keyword to access them.
def newSearch():
global barE_var

Related

If I created widgets in one function, how can I access them in another function using Python Tkinter?

this is my first project using Tkinter so please excuse me if the question is very simple to solve.
Depending on what option the user chooses from a dropdown, I call a function that creates and places certain widgets (e.g. an entry) on a frame. Then, when another button is pushed I want to access the text inside this entry. However, this seems to be giving me errors (saying that the widget is undefined) because I want to access a widget which I create when a function is called.
An obvious solution I see is to create all possible widgets I want to use outside the function, and only place them when the function is called. This seems quite sloppy and creates many more issues. Is there another fix?
Thanks in advance!
This is the function where I create and place the widgets on the frame.
def loadBook():
print("book is loaded")
#Authors
labelAuth1 = tk.Label(frame, text="Author 1 Name:")
entryAuth1 = tk.Entry(frame)
labelAuth1.place(relwidth=0.23, relheight=0.08, rely=0.1)
entryAuth1.place(relheight=0.08, relwidth=0.18, relx=0.3, rely=0.1)
This is a snippet of a function which uses input from the entry widget I created above:
def isBook():
if len(entryAuthSur1.get())==0:
pass
else:
bookString = ""
bookString += entryAuthSur1.get()
When the second function executes, I get a runtime error that entryAuthSur1 is not defined.
All variables inside functions are local. That means that it is deleted after the function call ends. As your variable (entryAuth1) wasn't global so it only existed inside the function and was deleted when the loadBook function ended. This is the working code:
import tkinter as tk
# Making a window for the widgets
root = tk.Tk()
def loadBook():
global entryAuth1 # make the entry global so all functions can access it
print("book is loaded")
#Authors
labelAuth1 = tk.Label(root, text="Author 1 Name:")
entryAuth1 = tk.Entry(root)
# I will check if the user presses the enter key to run the second function
entryAuth1.bind("<Return>", lambda e: isBook())
labelAuth1.pack()
entryAuth1.pack()
def isBook():
global entryAuth1 # make the entry global so all functions can access it
# Here you had `entryAuthSur1` but I guess it is the same as `entryAuth1`
if len(entryAuth1.get())==0:
pass
else:
bookString = ""
bookString += entryAuth1.get()
print(bookString) # I am going to print the result to the screen
# Call the first function
loadBook()
root.mainloop()

Python, Tkinter - Making get() dynamic

Below is some code that I'm testing with. In this code, I am able to create a window and have a Label on top and a Entry field on bottom. When I type in that entry field, I can dynamically change what's in the label. Now, I have included a function that is trying to evaluate a variable assigned to "tex", by storing what is predefined in the Entry widget. Which is "cat". This is picked up by:
tex = e.get()
I understand that get() is not changing dynamically as I change the text in the entry widget. So it cannot change to "dog" when I change the string in the entry widget. Is this possible? Here is the code:
from Tkinter import *
import time
root = Tk()
def change():
if tex == ("cat"):
time.sleep(0.5)
pass
else:
time.sleep(0.5)
e.delete(0, END)
e.insert(0, "dog")
v = StringVar()
e = Entry(root, textvariable=v)
e.insert(0, "cat")
e.pack(side=BOTTOM)
tex = e.get() #When text is defined with get(), it does not change
#dynamically with the entry widget
l = Label(root, textvariable=v)
l.pack(side=TOP)
change()
root.mainloop()
Any help would be appreciated.
To answer your specific question, no, there is no way for tex to magically keep updated when the entry widget changes. That feature is exactly why the textvariable attribute exists -- it causes the variable to always be updated with the value in the entry widget. If you need the data in another variable, you will have to call the .get() method.
That being said, you can set a trace on the variable, or a binding on the entry widget, and have it call code that can update tex to whatever you want when the entry widget changes.
For example, the following code shows how to have callback called whenever the value in the entry widget changes:
def callback(*args):
global tex
tex = e.get()
v.trace("w", callback)
For more information about what arguments are passed to the callback, see What are the arguments to Tkinter variable trace method callbacks?
All that being said, you have a couple of critical flaws in your code. First, you are creating the StringVar incorrectly. It needs to be v = StringVar() (note the trailing ()).
Also, you should never call sleep in the main thread of a GUI program. Read up on the after method if you want to execute some code after some time has elapsed.

Updating TKinter (Python) label with StringVar() - Variable not defined error (possible scope issue?)

Okay so I am trying to, every time the user updates a value in an entry box, calculate a new value from the input and display it with a label.
I'm having a few issues however, not matter how I do it, by binding a StringVar() variable to the label and updating that via the .set() method. Or by using .config(text="") method of the label itself. It throws me an error saying that either my stringVar() variable hasn't been defined or that the label isn't defined.
Here's a simplified version of my code:
def calculateFreqResolution ():
#calculate stuff from user input
N=numSamplesTxt.get() # number of samples
Fs=freqTxt.get() #sampling frequency
N=int(N) #cast them as ints
Fs=int(Fs)
res=Fs/N
###After done calculating display it
freqRes.set(res) #DOESN'T LIKE THIS LINE
def callbackNumSamples (numSamples):
##code here validates input into entry box, if valid then calculates then calls calculateFreqResolution()
calculateFreqResolution()
def callbackFreq (frequency):
##code here validates input into entry box, if valid then calculates then calls calculateFreqResolution()
calculateFreqResolution()
root=Tk()
freqRes=StringVar()
freqRes.set(1)
freqResCalcLabel=Label(root, textvariable=freqRes)
freqResCalcLabel.grid(row=5, column=1, pady=2, padx=6)
frequency=StringVar()
frequency.trace("w", lambda name, index, mode, frequency=frequency: callbackFreq(frequency))
freqTxt=Entry(root,textvariable=frequency, justify=CENTER)
freqTxt.insert(0, 1000)
numSamples=StringVar()
numSamples.trace("w", lambda name, index, mode, numSamples=numSamples: callbackNumSamples(numSamples))
numSamplesTxt=Entry(root, textvariable=numSamples, justify=CENTER)
numSamplesTxt.insert(0,1000)
root.mainloop()
The error I get is 'freqRes' has not been defined, despite me defining it as I have done here.
In this simplified version of the code it also gives me an error saying that 'numSamplesTxt' isn't defined when trying to use the .get() method. I have no idea why it works with my main code and not this code, but I'm assuming it's a similar issue that it has something to do with the scope of the objects?
The problem is that you are setting up the traces before you've initialized all of your variables. Move the traces toward the bottom of your script:
...
frequency=StringVar()
numSamples=StringVar()
freqTxt=Entry(...)
numSamplesTxt=Entry(...)
...
frequency.trace("w", ...)
numSamples.trace("w", ...)
root.mainloop()

More on passing arguments to tkinter button command

I have a number of test files in a single directory. I'm trying to write a GUI to allow me to select and run one of them.
So, I have a loop that scans the directory and creates buttons:
for fnm in glob.glob ('Run*.py'):
tstName = fnm[3:-3] # Discard fixed part of filename
btn = Button (self, text=tstName,
command=lambda: self.test(tstName))
btn.grid (row=rowNum, column=0, pady=2)
rowNum += 1
This creates my GUI correctly, with buttons labelled say, A and B but when I press on the button labelled A it passes B to the test method.
I've looked around and found this question How can I pass arguments to Tkinter button's callback command? but the answer doesn't go on to use the same variable name, with a different value, to configure another widget. (In fact, by tying the variable name to the widget name it almost implies that the technique won't work in this case, as I've found.)
I'm very new to Python, but am quite familiar with creating this kind of GUI using Tcl/TK and I recognise this problem - the value of tstName is being passed when I press the button, but I want it to pass the value the variable had when I created it. I know how I'd fix that in Tcl/Tk - I'd define a command string using [list] at creation time which would capture the value of the variable.
How do I do the same in Python?
You need to bind the current value of tstName at the time you define the button. The way you're doing it, the value of tstName will whatever it is at the time you press the button.
To bind the value at the time that you create the button, use the value of tstName as the default value of a keyword parameter to the lambda, like so:
btn = Button(..., command=lambda t=tstName: self.test(t))

How to detect when an OptionMenu or Checkbutton change?

My tkinter application has several controls, and I'd like to know when any changes occur to them so that I can update other parts of the application.
Is there anything that I can do short of writing an updater function, and looping at the end with:
root.after(0, updaterfunction)
This method has worked in the past but I'm afraid that it might be expensive if there are many things to check on.
Even if I did use this method, could I save resources by only updating items with changed variables? If so, please share how, as I'm not sure how to detect specific changes outside of the update function.
Many tkinter controls can be associated with a variable. For those you can put a trace on the variable so that some function gets called whenever the variable changes.
Example:
In the following example the callback will be called whenever the variable changes, regardless of how it is changed.
def callback(*args):
print(f"the variable has changed to '{var.get()}'")
root = tk.Tk()
var = tk.StringVar(value="one")
var.trace("w", callback)
For more information about the arguments that are passed to the callback see this answer
To have an event fired when a selection is made set the command option for OptionMenu
ex.
def OptionMenu_SelectionEvent(event): # I'm not sure on the arguments here, it works though
## do something
pass
var = StringVar()
var.set("one")
options = ["one", "two", "three"]
OptionMenu(frame, var, *(options), command = OptionMenu_SelectionEvent).pack()
If you are using a Tkinter Variable class like StringVar() for storing the variables in your Tkinter OptionMenu or Checkbutton, you can use its trace() method.
trace(), basically, monitors the variable when it is read from or written to.
The trace() method takes 2 arguments - mode and function callback.
trace(mode, callback)
The mode argument is one of “r” (call observer when variable is read by someone), “w” (call when variable is written by someone), or “u” (undefine; call when the variable is deleted).
The callback argument is the call you want to make to the function when the variable is changed.
This is how it is used -
def callback(*args):
print("variable changed!")
var = StringVar()
var.trace("w", callback)
var.set("hello")
Source : https://dafarry.github.io/tkinterbook/variable.htm
This will print the dropdown selection to the console. but my suggestion is to avoid console in GUI based applications. create a text indicator and print output to it
use the function in below code
from tkinter import *
tk = Tk()
def OptionMenu_SelectionEvent(event):
print(var.get())
pass
var = StringVar(); var.set("one")
options = ["one", "two", "three"]
OptionMenu(tk, var, *(options), command = OptionMenu_SelectionEvent).pack()
tk.mainloop()

Categories

Resources