Error in tkinter GUI --invalid command name "2025099205632update_values" while executing - python-3.x

I am getting the error "invalid command name "2025099205632update_values"
while executing" in tkinter GUI code. I attached my code.I am using Spyder 4 and Python 3.6.
Please help me to solve this error.
This is my code. This display the random number on tkinter frame. at the interval of 1000 mill second.
main.py call the class object to start GUI
panel_meter.py create label frame and call the class "r_phase_parameter" in r_phase.py
In class r_phase_parameter global variable updated at 1000 ms. These are the random numbers.
"""
This is main.py file.
"""
from panel_meter import *
meter_Window = meter()
meter_Window.start()
#########This is panel_meter.py file######################################
from tkinter import *
from r_phase import *
vol_r=100
class meter:
def __init__(self):
self.r_list = [] #This is not used
self.vol_r=vol_r
self.col=0
self.root=Tk()
self.root.title('Meter')
self.root.config(relief="sunken", borderwidth = 3,background="#FFFFF0")
self.r_frame=LabelFrame(self.root,text="R Phase") #,width=220,height=70
self.r_frame.grid(row=0,column=1,padx=10,pady=10)
self.r_frame.configure(foreground="red")
self.r_frame.config(background="#FFFFF0")
self.r_phase_read=r_phase_parameter(self.root,self.r_frame,self.vol_r,self.r_list)
def start(self):
self.root.mainloop()
###########################r_phase.py#############################
from tkinter import *
import meter_globel
import random
class r_phase_parameter:
def __init__(self,root,frame,r_vol,r_list):
self.root=root
self.frame=frame
#self.frame.grid_propagate(0)
self.l1=Label(self.frame,text='Voltage=' + str(meter_globel.r_globel_voltage)+'V',width=meter_globel.global_label_width)
self.l1.grid(row=0,column=0,padx=10,pady=10)
self.l1.config(background="#FFFFF0")
self.l1.after(meter_globel.update_time,self.update_values)
self.l2=Label(self.frame,text='Current=' + str(meter_globel.r_globel_current)+'A',width=meter_globel.global_label_width)
self.l2.grid(row=0,column=1,padx=10,pady=10)
self.l2.config(background="#FFFFF0")
self.l3=Label(self.frame,text='Frequency=' + str(meter_globel.r_globel_frequency)+'Hz',width=meter_globel.global_label_width)
self.l3.grid(row=0,column=2,padx=10,pady=10)
self.l3.config(background="#FFFFF0")
def __del__(self):
self.after_cancel(self.after_id)
def update_values(self):
meter_globel.r_globel_voltage=random.randrange(120,290)
meter_globel.r_globel_current=random.randrange(1,14)
meter_globel.r_globel_frequency=random.randrange(1,55)
if meter_globel.r_globel_voltage > 250:
bgc_rv='red'
else :
bgc_rv="Blue"
if meter_globel.r_globel_current > 10:
bgc_rc='red'
else :
bgc_rc="Blue"
if meter_globel.r_globel_frequency > 50:
bgc_rf='red'
else :
bgc_rf="Blue"
self.l1.configure(text='Voltage=' + str(meter_globel.r_globel_voltage)+'V',fg=bgc_rv,width=meter_globel.global_label_width)
self.l2.configure(text='Current=' + str(meter_globel.r_globel_current)+'A',fg=bgc_rc,width=meter_globel.global_label_width)
self.l3.configure(text='Frequency=' + str(meter_globel.r_globel_frequency)+'Hz',fg=bgc_rf,width=meter_globel.global_label_width)
self.l1.after(meter_globel.update_time,self.update_values)
##################This meter_global.py############################
r_globel_voltage=000
r_globel_current=00
r_globel_frequency=0
global_label_width= 12
update_time =1000 # This is in mill second

First of all, you must give your code clearer, I cannot reproduce your error, it works fine for me. However there were some attributeErrors but I fixed it.
I just brought up all together your code into single script.
from tkinter import *
import random
#GLOBAL VARIABLES
vol_r=100
r_globel_voltage=000
r_globel_current=00
r_globel_frequency=0
global_label_width= 12
update_time =1000 # This is in mill second
class r_phase_parameter:
def __init__(self,root,frame,r_vol,r_list):
self.root=root
self.frame=frame
#self.frame.grid_propagate(0)
self.l1=Label(self.frame,text='Voltage=' + str(r_globel_voltage)+'V',width=global_label_width)
self.l1.grid(row=0,column=0,padx=10,pady=10)
self.l1.config(background="#FFFFF0")
self.l1.after(update_time,self.update_values)
self.l2=Label(self.frame,text='Current=' + str(r_globel_current)+'A',width=global_label_width)
self.l2.grid(row=0,column=1,padx=10,pady=10)
self.l2.config(background="#FFFFF0")
self.l3=Label(self.frame,text='Frequency=' + str(r_globel_frequency)+'Hz',width=global_label_width)
self.l3.grid(row=0,column=2,padx=10,pady=10)
self.l3.config(background="#FFFFF0")
def __del__(self):
#################################
#I have changed here, itself of of this class is not inherited any tkinter
# widget so it cannot use self.after_cancel
#################################
self.root.after_cancel(self.update_values)
def update_values(self):
r_globel_voltage=random.randrange(120,290)
r_globel_current=random.randrange(1,14)
r_globel_frequency=random.randrange(1,55)
if r_globel_voltage > 250:
bgc_rv='red'
else :
bgc_rv="Blue"
if r_globel_current > 10:
bgc_rc='red'
else :
bgc_rc="Blue"
if r_globel_frequency > 50:
bgc_rf='red'
else :
bgc_rf="Blue"
self.l1.configure(text='Voltage=' + str(r_globel_voltage)+'V',fg=bgc_rv,width=global_label_width)
self.l2.configure(text='Current=' + str(r_globel_current)+'A',fg=bgc_rc,width=global_label_width)
self.l3.configure(text='Frequency=' + str(r_globel_frequency)+'Hz',fg=bgc_rf,width=global_label_width)
self.l1.after(update_time,self.update_values)
class meter:
def __init__(self):
self.r_list = [] #This is not used
self.vol_r=vol_r
self.col=0
self.root=Tk()
print("x")
self.root.title('Meter')
self.root.config(relief="sunken", borderwidth = 3,background="#FFFFF0")
self.r_frame=LabelFrame(self.root,text="R Phase") #,width=220,height=70
self.r_frame.grid(row=0,column=1,padx=10,pady=10)
self.r_frame.configure(foreground="red")
self.r_frame.config(background="#FFFFF0")
self.r_phase_read=r_phase_parameter(self.root,self.r_frame,self.vol_r,self.r_list)
def start(self):
self.root.mainloop()
meter_Window = meter()
meter_Window.start()
However, when I run this code, it shows this window, no errors and it changes dynamically so I assume it works fine.

Related

NameError: name 'Acte_D ' is not defined

I am currently developing a program to try an xml file. However I encounter a problem that has been blocking me for a few days now and I can rack my brains my beginner level in python and my many research does not help me to get over it.
I first try to retrieve 1 element from my xml file. I can do it well only the graphics window that should normally display it does not generate and I have the following error message:
The error :
window.fenetre_text(Acte_D) NameError: name 'Acte_D' is not defined`
well my code is :
import tkinter as tk
from tkinter import *
from tkinter import messagebox
from tkinter.filedialog import askopenfilename
from xml.dom import minidom`
global file
global Acte_D
global element_A
global lines
class fenetreM(Tk):
#GUI
def __init__(self, master=None,*args, **kwargs):
Tk.__init__(self,master, *args, **kwargs)
self.menu_bar()
self.title("EssaiV1.0.1")
self.geometry("800x400")
self.minsize(380,140)
self.config(background='#F0F0F2')
#New_frame=Frame(fenetreM, bg='#abd7f4')`
def menu_bar(self):
menu_bar = Menu(self)
menu_file = Menu(menu_bar, tearoff=0)
menu_file.add_command(label="Open", underline=0, command=self.open_file)
menu_file.add_command(label="Affichage",underline=0,command=self.do_something)
menu_file.add_separator()
menu_file.add_command(label="Exit",underline=1,command=self.quit)
menu_bar.add_cascade(label="File",underline=0, menu=menu_file)
menu_help = Menu(menu_bar, tearoff=0)
menu_help.add_command(label="About", command=self.do_about)
menu_bar.add_cascade(label="Help",underline=0, menu=menu_help)
self.config(menu=menu_bar)
valid = False
return(valid)
def open_file(self):
types = [("All Files",".*")]
file = askopenfilename(title="File to open : ", filetypes=types)
print("Copy")
doc = minidom.parse(file)
print(doc.nodeName)
print(doc.firstChild.tagName)
Acte_D = doc.getElementsByTagName("ActeDescription")
print("\n")
element_A = (f" we have {Acte_D.length} element \n Version : ")
for i in Acte_D:
screen_1=(i.getAttribute("version"))
print(screen_1)
print("\n")
return (Acte_D)
def fenetre_text(self,Acte_D):
tk=Text(fenetreM, height = 10, width =100)
Fenetre_A=tk.inset(END,textvariable=self.element_A+Acte_D)
Fenetre_A.pack(side="top", fill="x", expand=True)
return(Acte_D)
window = fenetreM()
window.fenetre_text(Acte_D)
window.mainloop()
Can someone explain my mistake to me?
Thanks
I tried to graphically display the result of Acte_D in my window defined in my method fenetre_text.
But I can't.

how to remove Recursion-Error in python tkinter?

i am trying to build a calculator (initial stages) using tkinter-python
when calling the run function of object its giving an [RecursionError: maximum recursion depth exceeded]enable to find why and where it is calling the run function repeatedly
import tkinter as gui
from tkinter import ttk
class box(gui.Tk):
def __init__(self):
#op = operators()
root = gui.Tk()
root.title("Calculator")
self.frame=ttk.Frame(root).grid()
self.display=ttk.Entry(text='0',font=('Comic Sans MS',20))
self.display.grid(row=0, columnspan=6)
self.buttons()
def buttons(self):
#printing numbers
self.seven=ttk.Button(text="7",command=lambda:self.getdata(7))
self.seven.grid(row=1,column=0)
self.eighth=ttk.Button(text="8",command=lambda:self.getdata(8))
self.eighth.grid(row=1,column=1)
self.nine=ttk.Button(text="9",command=lambda:self.getdata(9))
self.nine.grid(row=1,column=2)
self.four=ttk.Button(text="4",command=lambda:self.getdata(4))
self.four.grid(row=2,column=0)
self.five=ttk.Button(text="5",command=lambda:self.getdata(5))
self.five.grid(row=2,column=1)
self.six=ttk.Button(text="6",command=lambda:self.getdata(6))
self.six.grid(row=2,column=2)
self.one=ttk.Button(text="1",command=lambda:self.getdata(1))
self.one.grid(row=3,column=0)
self.two=ttk.Button(text="2",command=lambda:self.getdata(2))
self.two.grid(row=3,column=1)
self.three=ttk.Button(text="3",command=lambda:self.getdata(3))
self.three.grid(row=3,column=2)
def getdata(self,x):
self.num=x
def operate(self,y):
#add code for operators
self.getdata('op')
def run(self):
self.mainloop()
app=box()
app.run()

How to insert text in tkinter GUI while code is running

Hi I am try to create an Python GUI using tkinter package. Everything working perfectly but I want to insert or print some text while code is running. My code is little length process, I did not include all the code, to execute i need some update information on the Text area, so that user know coding is running and getting some information from internet.
Could you please kindly check this code. In this code number will insert in the Tkinter GUI and will be increase continuously. But i want to inset text form the given list. How can I insert text form the list. Please kindly help.
from tkinter import *
import threading
import queue
from time import sleep
import random
import tkinter as tk
list1 = ['Text 1', 'Text 2','Text 3','Text 4','Text 5','Text 6','Text 7',
'Text 8','Text 9','Text 10','Text 11']
class Thread_0(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
count = 0
while True:
count+=1
hmi.thread_0_update(count)
sleep(random.random()/100)
class HMI:
def __init__(self):
self.master= tk.Tk()
self.master.geometry('200x200+1+1')
f=tk.Frame(self.master)
f.pack()
self.l0=tk.Label(f)
self.l0.pack()
self.q0=queue.Queue()
self.master.bind("<<Thread_0_Label_Update>>",self.thread_0_update_e)
def start(self):
self.master.mainloop()
self.master.destroy()
def thread_0_update(self,val):
self.q0.put(val)
self.master.event_generate('<<Thread_0_Label_Update>>',when='tail')
def thread_0_update_e(self,e):
while self.q0.qsize():
try:
val=self.q0.get()
self.l0.config(text=str(val))
# self.l0.config(text=val)
except queue.Empty:
pass
##########################
if __name__=='__main__':
hmi=HMI()
t0=Thread_0()
t0.start()
hmi.start()

why a thread does not end

I'm trying to make an app to interact with an usb rfid reader on a Raspberry pi.
it looks like the solution is using the trheading module (otherwise the data entry leads to a disfunction)
After some essays I made an app that works... the first time. After that, a message error says "threads can only be started once"... so the interpretation is that the thread does not end naturally...why??
import time
import sys
import os
from tkinter import *
from tkinter.ttk import *
import threading
def activa_fil(self):
print ('activant fil')
fil.start()
def prova2():
print('hola')
print(Control.get())
print(NumCorr.get())
print(time.strftime("%H:%M:%S"))
DisplayCorr.config(text=NumCorr.get())
Llistacorr.insert(0, (Control.get(),NumCorr.get(), time.strftime("%H:%M:%S")))
marc.contador +=1
Contador.config (text=marc.contador)
print(marc.contador)
file = open("prova.txt", "a")
file.write(Control.get())
file.write(NumCorr.get())
file.write (time.strftime("%H:%M:%S"))
file.write('\n')
file.close()
NumCorr.after(500, NumCorr.delete(0,END))
def tick():
time_string = time.strftime("%H:%M:%S")
Hora.config(text=time_string)
Hora.after(200, tick)
def canvia_focus(self):
NumCorr.focus()
fil=threading.Timer(0.5, prova2)
marc = Tk()
marc.geometry('480x320')
marc.bind_all(''<'Key-Return'>'', activa_fil)
marc.contador =0
Control = Combobox(marc)
Control['values']=('sortida','can cuias','can campanya','papiol','sortida papiol', 'can olivero', 'ullastrell', 'coll olesa','pla fideuer','aeri', 'arribada')
Control.config(font="helvetica 30", )
Control.grid(column='0', row='0', columnspan='2')
Llistacorr = Listbox(marc)
Llistacorr.config(font='arial')
Llistacorr.grid_propagate(0)
Llistacorr.grid(column='0', row='1', rowspan='3')
DisplayCorr=Label(marc, font='Arial 10')
DisplayCorr.grid(column='1', row='1')
NumCorr = Entry(marc)
NumCorr.config(font='helvetica 6')
Control.bind('<<ComboboxSelected>>', canvia_focus)
NumCorr.grid(column='0', row='4', columnspan='2')
Contador=Label(marc, font='arial 30')
Contador.grid(column='1', row='2')
Hora=Label(marc, font='arial 30')
Hora.grid(column='1', row='3')
tick()
marc.mainloop()

How to thread with tkinter in python3 using queue?

I'm trying to thread definitions in tkinter using queue in specifically python3
I've had similar code in python2 work great using a similar method without queue but in python3 from what i've read tkinter doesn't allow for multithreading with gui. I found some examples that uses Queue process. They outline i'm suppose to create an Queue object, a new thread with access to that queue and check for input in the main thread
#!/usr/bin/python3
from tkinter import *
import time
import threading
import queue
import subprocess
def temp_sensor(queue_list):
warning = 0
while True:
var = "cat /sys/class/thermal/thermal_zone*/temp"
temp_control = subprocess.check_output([var], shell=True)
temp_length = len(temp_control)
temp_control = temp_control[35:]
temp_control = temp_control[:-4]
temp_control = int(temp_control)
degree_sign= u'\N{DEGREE SIGN}'
displayed_temp = "Tempature: " + str(temp_control) + degree_sign + "C"
if temp_control > 79:
warning = warning + 1
if warning == 3:
print ("Warning Core Tempature HOT!")
warning = 0
if temp_control > 90:
time.sleep(3)
print ("Warning EXTREMLY to HOT!!!")
queue_list.put(displayed_temp)
time.sleep(1)
class Gui(object):
def __init__(self, queue_list):
self.queue_list = queue_list
self.root = Tk()
self.root.geometry("485x100+750+475")
main_tempature_status = StringVar(self.root)
Ts = Entry(self.root, textvariable=main_tempature_status)
Ts.pack()
Ts.place(x=331, y=70, width=160, height=25)
Ts.config(state=DISABLED, disabledforeground="Black")
self.root.after(1000, self.read_queue)
def read_queue(self):
try:
temp = self.queue.get_nowait()
self.main_tempature_status.set(temp)
except queue_list.Empty:
pass
self.root.after(1000, self.read_queue)
if __name__ == "__main__":
queue_list = queue.Queue()
gui = Gui(queue_list)
t1 = threading.Thread(target=temp_sensor, args=(queue_list,))
t1.start()
gui.root.mainloop()
My desired result is to run a some of these definitions to do various tasks and display their variables in the tkinter entry using python3.
when i run my code it gives me the variable from the queue but it won't post to the GUI. please forgive my less then pythonic code.
Change you Gui class to this:
class Gui(object):
def __init__(self, queue_list):
self.queue_list = queue_list
self.root = Tk()
self.root.geometry("485x100+750+475")
self.main_tempature_status = StringVar(self.root)
self.Ts = Entry(self.root, textvariable=self.main_tempature_status)
self.Ts.pack()
self.Ts.place(x=331, y=70, width=160, height=25)
self.Ts.config(state=DISABLED, disabledforeground="Black")
self.root.after(1000, self.read_queue)
def read_queue(self):
try:
temp = self.queue_list.get_nowait()
self.Ts.config(state=NORMAL)
self.main_tempature_status.set(temp)
self.Ts.config(state=DISABLED)
except queue.Empty:
pass
self.root.after(1000, self.read_queue)
Explanation:
variable main_temperature_status is used in function read_queue as class variable, but not defined as class variable.
You cannot show the change in value of Entry widget if it is always disabled, so enabling it before value change in read_queue.

Resources