Label not changing in window - tkinter - python

I have this code that I'm following from a video. This is a function that gets activated when a button is pressed. In some parts I want to erase the previous output in a label every time the button is pressed:
# Search customers
def search_customers():
search_customers = Tk()
search_customers.title("Search Customers")
search_customers.geometry("1300x600")
searched_label = Label(search_customers)
searched_label.grid(row=2, column=0)
test = Label(search_customers)
test.grid(row=3, column=0)
def search_now():
# searched_label = Label(search_customers)
# searched_label.grid(row=2, column=0)
selected = drop.get() # This is a Combobox
if selected == 'Search By...':
sql = ""
test['text'] = 'You forgot to pick an option'
elif selected == 'Last Name':
sql = "SELECT * FROM customers WHERE last_name = %s"
elif selected == 'Email Address':
sql = "SELECT * FROM customers WHERE email = %s"
elif selected == 'Customer ID':
sql = "SELECT * FROM customers WHERE user_id = %s"
searched = search_box.get()
name = (searched, )
result = my_cursor.execute(sql, name)
if selected == "Search By...":
result = ''
else:
result = my_cursor.fetchall()
if not result:
result = "Record Not Found"
test['text'] = ''
searched_label['text'] = result
elif result:
test['text'] = ''
searched_label['text] = ''
searched_label = Label(search_customers)
for index, x in enumerate(result):
num = 0
index += 2
for y in x:
searched_label = Label(search_customers, text=y)
searched_label.grid(row=index, column=num)
num += 1
The thing is, every time the code reaches this statement: searched_label['text'] = '', it says: variable referenced before the assignment but that doesn't happen with test['text'] = '' even though both labels are created in the same scope.
The only way it worked was to create searched_label inside the search_now() (see the commented lines and let's pretend to uncomment them and comment the ones above).
With the lines uncommented inside search_now(), when it reaches this statement: if not result, it sets searched_label['text'] = result without a problem, but when it reaches the last elif, it doesn't set searched_label['text'] = '', actually, let's say the code was run and it first reached the if not result: statement so when the button is press again and it reaches the last elif it doesn't erase the previous output with searched_label['text] = ''.
In this last elif, I tried reached_label.grid_remove() and creating the label again but the previous output still remains so it mixes with the new output.
Thanks in advance, I'm still learning and I hope my question is clear enough

If you want to change the texts of Label widgets regularily, it pays to use the parameter textvariable. It takes a StringVar() object that can be changed by any function. As soon as the StringVar gets a new value, the label changes.
I do not see the whole of your program, but this is how it works in general:
def search_customers():
search_customers = Tk()
search_customers.title("Search Customers")
search_customers.geometry("1300x600")
global labeltext
labeltext = StringVar() ## Initiate a string variable
labeltext.set("This is the mutable text")
searched_label = Label(search_customers,textvariable=labeltext)
searched_label.grid(row=2, column=0)
test = Button(search_customers,text="Change it",command=change)
test.grid(row=3, column=0)
def change():
labeltext.set("This is a new text")
If the program gets more complicated, you might also consider defining the dialog box as a new class, iheriting from Frame. There, you can define methods that have access to all widgets and variables without the need of global variables.

I can't help you. Actually, need more information. I am using match case statements instead of if/else.
Code:
def search_now():
searched_label = Label(search_customers)
searched_label.grid(row=2, column=0)
selected = drop.get() # This is a Combobox
match selected:
case 'Search By...':
sql = ""
test['text'] = 'You forgot to pick an option'
case 'Last Name':
sql = "SELECT * FROM customers WHERE last_name = %s"
case 'Email Address':
sql = "SELECT * FROM customers WHERE email = %s"
case 'Customer ID':
sql = "SELECT * FROM customers WHERE user_id = %s"
searched = search_box.get()
name = (searched, )
result = my_cursor.execute(sql, name)
if selected == "Search By...":
result = ''
else:
result = my_cursor.fetchall()
if not result:
result = "Record Not Found"
#test['text'] = ''
searched_label.config{text=result)
elif result:
#test['text'] = ''
searched_label.config(text='')
#searched_label = Label(text=search_customers)
for index, x in enumerate(result):
num = 0
index += 2
for y in x:
#searched_label = Label(search_customers, text=y)
searched_label.config(text=y)
Let me know if this work.

Related

using entry.get in a list count in python

I try to use the ID entry from the GUI to count the similar IDs in the Excel column.
I always get a 0 in the if-loop and red color shows.
But there are similar IDs in the column.
My code
l1 = tk.Label(tab2, text="Status Check")
l1.place(x=10, y=10)
l2 = tk.Label(tab2, text="ID")
l2.place(x=10, y=60)
ID = tk.Entry(tab2)
ID.place(x=80, y=60)
l2 = tk.Label(tab2, text="Status")
l2.place(x=10, y=100)
t1 = tk.Entry(tab2)
t1.place(x=80, y=100)
comment = tk.Label(tab2)
comment.place(x=240, y=100)
df = pd.read_excel(r'Excel.xlsx')
IDlist = df['ID'].tolist()
id = ID.get()
def immunity_check():
d = IDlist.count(id)
print(d)
if d >= 2:
t1.config(bg= "Green")
comment.configure(text="Fully vaccinated!")
elif d == 1:
t1.config(bg= "Yellow")
comment.configure(text="Vaccinated!")
else d <= 0:
t1.config(bg= "Red")
comment.configure(text="Not vaccinated!")
Can anyone give an advice on how to fix it?
I totally agree with furas comment. Thank him, he solved it.
Issue
Currently the code is reading the input from your text-field before button is pressed. Place a print(id) behind the ID.get() statement and watch console, like:
# GUI initialization omitted for brevity
df = pd.read_excel(r'Excel.xlsx') # read from Excel before button-pressed
IDlist = df['ID'].tolist()
id = ID.get() # get input from text-field before button-pressed
print(id)
# wait for a button press
# below is called on button-press and uses previously read id as argument
def immunity_check():
Solution
This is how you could solve it. The id should be read from text-input after button was pressed. So put move statement into the method:
# part 1: GUI initialization omitted for brevity
# part 2: define functions to call later
def read_ids():
df = pd.read_excel(r'Excel.xlsx')
return df['ID'].tolist()
def immunity_check():
id = ID.get() # read the id to search/count
d = id_list.count(id)
print(f"occurrences of id '{id}' in list: {d}")
if d >= 2:
t1.config(bg= "Green")
comment.configure(text="Fully vaccinated!")
elif d == 1:
t1.config(bg= "Yellow")
comment.configure(text="Vaccinated!")
else d <= 0:
t1.config(bg= "Red")
comment.configure(text="Not vaccinated!")
# part 3: main starts
id_list = read_ids()
# add button with trigger to function immunity_check()
button = tk.Button(tab2,text="Check",command=immunity_check) button.place(x=10,y=180)

Python tkinter - Change the Label created by a loop, how can I do?

I read a lot for it and I have the sentiment that there is no solution for me.
Context: user has to enter 6-digit calculation number in the entries. The 9x entries are created by a loop. I wrote the condition to detect if the user did a mistake : number which contains less or more than 6 digit or / and contains a letter. They are working, I tested them. Only 6-digit number are accepted (if 0, it's ok since it means that the user just need to get less than 9x documents).
Goal: if the user did one mistake, a message "Error in the number" at the place of the "0" has to appear right next to the concerned and "faulty" entry.
Everything will be activated through a button.
What I tried: with a list, but it doesn't work.
How can I change the Label dynamically created by the loop ?
user_entries=[]
error_list_length=[0,0,0,0,0,0,0,0,0]
error_list_letters=[0,0,0,0,0,0,0,0,0]
error_calculation_list=[0,0,0,0,0,0,0,0,0]
nbofcalc=9
a=0
b=1
ddd=791250
#------ Function to check if the entered calculation number are composed of 6 characters (mandatory)
def check_calc_number():
global gg_error
gg=0
gg_error=0
while gg<nbofcalc:
if len(user_entries[gg].get()) != 6:
if len(user_entries[gg].get()) ==0:
gg_error+=0
error_list_length[gg]=0
else:
gg_error+=1
error_list_length[gg]=1
else:
gg_error+=0
error_list_length[gg]=0
gg+=1
#------ Function to check if the entered calculation number contains a or many letters (prohibited)
def check_calc_letter():
global hh_error
hh=0
hh_error=0
alphabet_x='a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'
for x in range(nbofcalc):
for n in user_entries[hh].get():
if n in alphabet_x:
hh_error+=1
error_list_letters[hh]=1
hh+=1
#------ Function to check with entries has an error and update the list "error_calculation_list" to try to displays the message "Error in the number" next to the concerned entries
def error_length_letters_display():
ww=0
while ww<nbofcalc:
if error_list_length[ww]==1 :
error_calculation_list[ww]="Error"
var_calculation.set(error_calculation_list[ww])
if error_list_letters[ww]==1:
error_calculation_list[ww]="Error"
var_calculation.set(error_calculation_list[ww])
ww+=1
#----- Loop to crate the entries and Label
for x in range(nbofcalc):
cadre1=Frame(fenetre)
cadre1.pack(side=TOP,anchor=NW)
cadre=Frame(cadre1)
cadre.pack()
c=str(b)
calc=Label(cadre,text="Calculation "+c+" ")
calc.pack(side=LEFT)
var_entry=StringVar()
my_entry=Entry(cadre,textvariable=var_entry, bd=5)
my_entry.insert(0,ddd)
my_entry.pack(side=LEFT)
var_calculation=StringVar()
var_calculation.set(error_calculation_list[a])
calc_error_frame=Label(cadre, textvariable=var_calculation) # The Label to change if error
calc_error_frame.pack(side=RIGHT)
user_entries.append(my_entry)
a+=1
b+=1
ddd+=1
Thank you !
You can store values and objects you need to change, so then you can change them dinamically. See this example
from tkinter import *
r = Tk()
my_entries = []
for i in range(5):
e = Label(r, text=i)
my_entries.append(e)
e.pack(side='top')
r.after(4000, lambda: my_entries[2].configure(text='Example'))
r.mainloop()
EDIT 1:As TheLizzard and Cool Cloud pointed out, it's better to avoid using time.sleep() while using tkinter. Replaced with non-blocking after()
you could setup the trace function for tk.StringVar(), when user enter any value into Entry, it will be checked. for example as shown on below user only can type decimal, and you and setup length as well.
def create_widgets(self):
self.vars = tk.StringVar()
self.vars.trace('w', partial(self.validate2, 1, 1))
# Min Voltage Validating Part
self.vars1 = tk.StringVar()
self.vars1.trace('w', partial(self.validate2, 2, 2))
# Max Voltage Validating Part
self.vars2 = tk.StringVar()
self.vars2.trace('w', partial(self.validate2, 3, 4))
# Current Validating Part
self.vars3 = tk.StringVar()
self.vars3.trace('w', partial(self.validate2, 4, 3))
# Channel Validating Part
# function( key, size)
self.enter_info = tk.Label(self, text="Please enter your information: ", bg="lightgrey")
self.enter_info.grid(tke_Table_EnterInfo)
self.voltage = tk.Label(self)
self.voltage["text"] = "MinVoltage"
self.voltage.grid(tke_Label_MinVoltage)
self.voltageInput = tk.Entry(self, width=10, textvariable=self.vars).grid(tke_StringBox_MinVoltage)
self.vars.set(0)
# Min Voltage Validating Part
self.current = tk.Label(self)
self.current["text"] = "MaxVoltage"
self.current.grid(tke_Label_MaxVoltage)
self.currentInput = tk.Entry(self, width=10, textvariable=self.vars1).grid(tke_StringBox_MaxVoltage)
self.vars1.set(5)
# Max Voltage Validating Part
self.power = tk.Label(self)
self.power["text"] = "Current"
self.power.grid(tke_Label_MaxCurrent)
self.powerInput = tk.Entry(self, width=10, textvariable=self.vars2).grid(tke_StringBox_MaxCurrent)
self.vars2.set(62.5)
# Max Current Validating Part
self.channel = tk.Label(self)
self.channel["text"] = "channel"
self.channel.grid(tke_Label_Channel)
self.channelInput = tk.Entry(self, width=10, textvariable=self.vars3).grid(tke_StringBox_Channel)
self.vars3.set(8)
# Max Channel Validating Part
def validate2(self, key, size, *args):
# TODO:add more information
if key == 1:
value = self.vars.get()
elif key == 2:
value = self.vars1.get()
elif key == 3:
value = self.vars2.get()
else:
value = self.vars3.get()
if not value.isdecimal():
print(len(value))
# if len(value) < 2:
corrected = ''.join(filter(str.isdecimal, value))
if key == 1:
self.vars.set(corrected)
elif key == 2:
self.vars1.set(corrected)
elif key == 3:
self.vars2.set(corrected)
else:
self.vars3.set(corrected)
if key == 1:
corrected = self.vars.get()
corrected = corrected[0:size]
self.vars.set(corrected)
elif key == 2:
corrected = self.vars1.get()
corrected = corrected[0:size]
self.vars1.set(corrected)
elif key == 3:
corrected = self.vars2.get()
corrected = corrected[0:size]
self.vars2.set(corrected)
else:
corrected = self.vars3.get()
corrected = corrected[0:size]
self.vars3.set(corrected)

How to continue the program outside the function?

I'm new to python and got stuck while trying to build a GUI. I can't find a way to extract data from the 'login' function, which would be the new TopLevel window created after the user logs in. Because of that, I have to write the remaining code inside the 'login function', but I have the impression that there must be another way around. I tried making the new top level global, but it returns that the new variable is not defined.
from tkinter import *
from tkinter import messagebox
root = Tk()
login_frame = LabelFrame(root, text = "login info").pack()
user_field = Label(login_frame, text = "user: ")
user_field.grid(row = 0,column = 0)
pass_field = Label(login_frame, text = "pass: ")
pass_field.grid(row = 1, column = 0)
user_input = Entry(login_frame)
user_input.grid(row = 0, column = 1)
pass_input = Entry(login_frame, show = "*")
pass_input.grid(row = 1, column = 1)
def login():
if user_input.get() == "user" and pass_input.get() == "user":
if messagebox.showinfo("blah", "blah") == "ok":
pass_input.delete(0, END)
user_input.delete(0, END)
root.withdraw()
**app = Toplevel()**
else:
messagebox.showerror("blah", "blah")
pass_input.delete(0, END)
user_input.delete(0, END)
login_btn = Button(login_frame, text = "LOGIN")
login_btn.grid(row = 2, column = 0)
exit_btn = Button(login_frame, text = "SAIR")
exit_btn.grid(row = 2, column = 1)
root.mainloop()
Your code is breaking indentation. The lines following the definition of the function must be inside the scope of the function, like this:
def login():
if user_input.get() == "user" and pass_input.get() == "user":
if messagebox.showinfo("blah", "blah") == "ok":
...
Regardless of that, you may return any type of data at the end of a function. Consider exposing your TopLevel app like this:
return TopLevel()

function not working using sql and tkinter (quiz)

On the function, confirmAnswer, it updates the question but doesn't display to the user whether their answer is correct or inccorrect which makes me think it is skipping straight to this line, if self.Qn < self.recordNum['text']: on the same function.
I only have two questions in the database at the moment allowing testing to be easier and when the question updates, the submit button doesnt work when an answer is inputted when really the quiz should end and display the score which is in the same function.
I included this much code as I thought it would make more sense to understand the code so sorry if it is too much and sorry if it comes across confusing. Thank you for your help!
def quiz(self):
self.newf.pack_forget()
self.head['text'] = 'Welcome to the psychology revision quiz'
self.quizf.pack()
self.quizScore = 0
self.correctAnswer = '' # <-- create it at start (and use better name)
self.Qn = 1
self.update_question()# <-- get new question
self.update_question_number()
def update_question_number(self):
# Get question's number
query = "SELECT MAX(qnumber) FROM questions"
c.execute(query)
row = c.fetchone()
self.recordNum['text'] = row[0]
def update_question(self):
# Get new question
query = "SELECT * FROM questions WHERE qnumber=?"
c.execute(query, (self.Qn,))
row = c.fetchone()
self.question['text'] = row[1]
self.answer1['text'] = row[2]
self.answer2['text'] = row[3]
self.answer3['text'] = row[4]
self.answer4['text'] = row[5]
self.correctAnswer = row[6]
def confirmAnswer(self):
self.rightOrWrong = self.enterAnswer
if self.enterAnswer == self.correctAnswer:
self.rightOrWrong['text'] = "Correct"
self.quizScore += 1
self.update_question()
else:
self.rightOrWrong['text'] = "Incorrect"
if self.Qn < self.recordNum['text']:
self.Qn += 1 # <-- get new question
self.update_question() # <-- get new question
else:
self.rightOrWrong['text'] = "Quiz Complete! Your score was: {}".format(self.quizScore)

How to increment the number in results[0]?

I have a Python/Tkinter program and I am trying to increment some numbers that are at the end of a variable used for tk.Entry boxes.
For example, self.output0, self.output1, self.output2, etc
(Sorry, my code is long so I tried shortening it up for an example.)
self.entries = []
self.output0 = tk.Entry(self, width=149, justify='center', bd='0', bg='#E0E0E0')
self.output0.grid(column=0, row=6, columnspan=5, padx=1)
self.output1 = tk.Entry(self, width=149, justify='center', bd='0', bg='#E0E0E0')
self.output1.grid(column=0, row=7, columnspan=5, padx=1)
I grab the users input with another entry box such as 'self.first_entry' and run a SQL query on the user input.
For example:
class Adder(ttk.Frame):
"""The adders gui and functions."""
def __init__(self, parent, *args, **kwargs):
ttk.Frame.__init__(self, parent, *args, **kwargs)
self.root = parent
self.init_gui()
def authenticate(self):
return pypyodbc.connect('Driver={SQL Server};Server=MYSERVERNAME;Database=MYDATABASENAME;Trusted_Connection=yes;')
def calculate(self):
firstname = str(self.first_entry.get()) # converts user entry into variable
lastname = str(self.last_entry.get())
license = str(self.lic_entry.get())
try:
connection = self.authenticate()
except pypyodbc.Error as ex:
sqlstate = ex.args[0]
if sqlstate == '28000':
self.output0.delete(0, END)
self.output0.insert(0,"You do not have access.")
cursor = connection.cursor()
if (firstname and not lastname and not license): # "You entered first name."
SQLCommand = ("SELECT LASTNAME, FIRSTNAME, LICNUMBER "
"FROM MyTABLEName " # table name
"with (nolock)"
"WHERE FIRSTNAME LIKE ?")
Values = [firstname + '%']
else:
SQLCommand = ("SELECT LASTNAME, FIRSTNAME, LICNUMBER "
"FROM MyTABLEName " # table name
"(nolock)"
"WHERE FIRSTNAME LIKE ? AND LASTNAME LIKE ? AND LICNUMBER LIKE ?")
Values = [firstname + '%', lastname + '%', '%' + license + '%']
cursor.execute(SQLCommand,Values)
results = cursor.fetchmany(10)
if results:
self.output0.delete(0, END) # clears entry
self.output0.insert(0,results[0]) # enters results in entry
self.output1.delete(0, END) # clears entry
self.output1.insert(0,results[1]) # enters results in entry
self.output2.delete(0, END) # clears entry
self.output2.insert(0,results[2]) # enters results in entry
self.output3.delete(0, END) # clears entry
self.output3.insert(0,results[3]) # enters results in entry
self.output4.delete(0, END) # clears entry
self.output4.insert(0,results[4]) # enters results in entry
connection.close()
else:
self.output0.delete(0, END)
self.output0.insert(0,"There are no records for this input.")
The above will output 5 rows from the database.
I am wanting to shorten the code up. How can I take the below and write it so that I do not have to repeat
self.output0.delete(0, END) # clears entry
self.output0.insert(0,results[0]) # enters results in entry
self.output1.delete(0, END) # clears entry
self.output1.insert(0,results[1]) # enters results in entry
2
2
3
3
...etc
I want to increment the outputX and the number in results[x]
This below is what I have been trying but it is not working:
cursor.execute(SQLCommand,Values)
results = cursor.fetchmany(10)
self.outputs = [self.output0, self.output1, self.output2, self.output3, self.output4]
for output in self.outputs:
for i in range(0, 4):
if results:
output.delete(0, END) # clears entry
output.insert(0,results[i]) # enters results in entry
else:
self.output0.delete(0, END)
self.output0.insert(0,"There are no records for this input.")
How can I increment the number in "resuls[x]" so that I get a different row from the SQL query? It keeps printing the same row on each line.
The problem is that you're trying to index your cursor. Try converting it to a list first and then running the function.
results = list(cursor.fetchmany(10))
One easy and not so intrusive solution would be to use getattr:
def update_fields(self, prefix, values):
for x, _ in enumerate(values):
attr = getattr(self, prefix + str(x))
attr.delete(0, END)
attr.insert(0, values[x])
And then you'd just:
results = cursor.fetchmany(10)
if results:
self.update_fields('output', results)
Please note that getattr will raise an AttributeError if self has no declared field named outputX
You can ignore undeclared fields by swallowing the exception:
def update_fields(self, prefix, values):
for x, _ in enumerate(values):
try:
attr = getattr(self, prefix + str(x))
attr.delete(0, END)
attr.insert(0, values[x])
except AttributeError:
pass
# You can also use continue

Categories