Hi,
I wrote a small app which have some different pages but now i have a problem with update dynamically a label text from one of the pages.
I would highly appreciate if anyone can have a look and maybe help me...

I want to update self.label1 from pag01 class text with variable ain0 value .

this is the code

import Tkinter
import tkMessageBox
from Tkinter import *
import MySQLdb
import sys
import datetime
from datetime import timedelta
import time
import calendar
import u3
import struct
import traceback
import uuid

d = u3.U3()
d.configU3()
d.getCalibrationData()
d.configIO(FIOAnalog = 255)
d.configIO(EIOAnalog=12)
d.configIO(NumberOfTimersEnabled = 2,TimerCounterPinOffset = 8)

t0Config = u3.TimerConfig(0, TimerMode = 2)
t1Config = u3.TimerConfig(1, TimerMode = 2)

d.getFeedback( t0Config, t1Config )

d.configTimerClock(TimerClockBase = 2, TimerClockDivisor = 48)

timer0 = u3.Timer(0)
timer1 = u3.Timer(1)

ainVoltage = [0]*8

ainVoltage[0] = d.getAIN(0, negChannel = 32)
ain0 = ainVoltage[0]

#connect to db
db = MySQLdb.connect(host="localhost",
                     user="***",
                     passwd="***",
                     db="***")
#create cursor
cur = db.cursor()

#set client related variables
cur.execute("SELECT * FROM users")
for row1 in cur.fetchall():
    client_id = row1[1]
    client_name = row1[2]
    client_username = row1[3]
    client_license = row1[4]
    client_license_expire = row1[5]
    client_last_connect_asv = row1[6]
    client_password = row1[7]

#util variables
today = datetime.date.today()
today_5 = today - timedelta(days=5)

#
class Pag00():

    def __init__(self,master):

        self.master=master
        self.master.geometry('1024x680+50+50')
        self.master.title('ASV Technologies ')
        self.master.configure(background='#CDC5D9')

        self.label1=Label(self.master,text='Sistem testare frane ASV',fg='blue', font=("Arial", 36, "bold"),background='#CDC5D9').grid(row=0,column=2)

        self.labelspatiere=Label(self.master,text='',font=("Arial", 18, "bold"),background='#CDC5D9').grid(row=1,column=2)
        self.labelspatiere=Label(self.master,text='                             ',font=("Arial", 18, "bold"),background='#CDC5D9').grid(row=0,column=0)
        self.labelspatiere=Label(self.master,text='',font=("Arial", 18, "bold"),background='#CDC5D9').grid(row=3,column=2)
        self.labelspatiere=Label(self.master,text='',font=("Arial", 18, "bold"),background='#CDC5D9').grid(row=5,column=2)
        self.labelspatiere=Label(self.master,text='',font=("Arial", 18, "bold"),background='#CDC5D9').grid(row=7,column=2)
        self.labelspatiere=Label(self.master,text='',font=("Arial", 18, "bold"),background='#CDC5D9').grid(row=9,column=2)
        self.labelspatiere=Label(self.master,text='',font=("Arial", 18, "bold"),background='#CDC5D9').grid(row=11,column=2)
        self.labelspatiere=Label(self.master,text='                    ',font=("Arial", 18, "bold"),background='#CDC5D9').grid(row=1,column=4)


        self.labelclienttxt=Label(self.master,text='Licensed to : ',font=("Arial", 18, "bold"),background='#CDC5D9').grid(row=2,column=5)
        self.labelclientname=Label(self.master,text=client_name,font=("Arial", 18, "bold"),fg='blue',background='#CDC5D9').grid(row=3,column=5)

        text_label_hardware = hardware_check(client_id)
        if hardware_check(client_id) == "OK":
            color_label_hardware = "#00C957"
        elif hardware_check(client_id) == "Invalid hardware":
            color_label_hardware = "#EE0000"
        self.labelhwinittext=Label(self.master,text='Hardware status ',font=("Arial", 18, "bold"),background='#CDC5D9').grid(row=4,column=5)
        self.labelhwinit=Label(self.master,text=text_label_hardware,font=("Arial", 18, "bold"),fg=color_label_hardware,background='#CDC5D9').grid(row=5,column=5)

        self.labellictext=Label(self.master,text='License validity : ',font=("Arial", 18, "bold"),background='#CDC5D9').grid(row=6,column=5)
        self.labellic=Label(self.master,text=client_license_expire,font=("Arial", 18, "bold"),fg='red',background='#CDC5D9').grid(row=7,column=5)

        text_label_status = status_check(client_last_connect_asv)
        if status_check(client_last_connect_asv) == "OK":
            color_label_status = "#00C957"
        elif status_check(client_last_connect_asv) == "System Problem":
            color_label_status = "#EE0000"
        self.labelstatustext=Label(self.master,text='System status : ',font=("Arial", 18, "bold"),background='#CDC5D9').grid(row=8,column=5)
        self.labelstatus=Label(self.master,text=text_label_status,font=("Arial", 18, "bold"),fg=color_label_status,background='#CDC5D9').grid(row=9,column=5)

        self.labellic2x4=Label(self.master,text='YES',font=("Arial", 18, "bold"),fg='#7CFC00',background='#CDC5D9').grid(row=2,column=3)
        self.labellic4x4=Label(self.master,text='YES',font=("Arial", 18, "bold"),fg='orange',background='#CDC5D9').grid(row=4,column=3)
        self.labelliccamion=Label(self.master,text='NO',font=("Arial", 18, "bold"),fg='red',background='#CDC5D9').grid(row=6,column=3)
        self.labellicmoto=Label(self.master,text='YES',font=("Arial", 18, "bold"),fg='orange',background='#CDC5D9').grid(row=8,column=3)

        self.button1=Button(self.master,text='Autoturism 2x4',fg='blue',font=('Helvetica',26),command=self.gotoPag01,height=1, width=15).grid(row=2,column=2)
        self.button2=Button(self.master,text='Autoturism 4x4',fg='blue',font=('Helvetica',26),command=self.gotoPag02,height=1, width=15).grid(row=4,column=2)
        self.button3=Button(self.master,text='Camion',fg='blue',font=('Helvetica',26),command=self.gotoPag02,height=1, width=15).grid(row=6,column=2)
        self.button4=Button(self.master,text='Moto',fg='blue',font=('Helvetica',26),command=self.gotoPag02,height=1, width=15).grid(row=8,column=2)
        self.button5=Button(self.master,text='Setari',fg='blue',font=('Helvetica',26),command=self.gotoPag01,height=1, width=15).grid(row=10,column=2)
        self.button6=Button(self.master,text='Exit',fg='blue',font=('Helvetica',26),command=self.exit,height=1, width=15).grid(row=12,column=2)

    def gotoPag01(self):
        root1=Toplevel(self.master)
        myGui=Pag01(root1)

    def gotoPag02(self):
        root2=Toplevel(self.master)
        myGui=Pag02(root2)

    def gotoPag03(self):
        root3=Toplevel(self.master)
        myGui=Pag03(root3)

    def gotoPag04(self):
        root4=Toplevel(self.master)
        myGui=Pag04(root4)

    def gotoPag05(self):
        root5=Toplevel(self.master)
        myGui=Pag05(root5)

    def exit(self):
        mExit = tkMessageBox.askyesno(title='Exit', message='Are you sure ?')
        if mExit > 0:
            self.master.destroy()
            return


class Pag01():
    def __init__(self,master):

        self.master=master
        self.master.geometry('1024x680+50+50')
        self.master.title('ASV Technologies - Test frane auto 2x4')
#Here i want my ain0 value 
        self.label1=Label(self.master,text=ain0,fg='red').grid(row=0,column=2)
        self.button2=Button(self.master,text='back',fg='blue',command=self.myquit).grid(row=5,column=3)

    def myquit(self):
        self.master.destroy()


#pag02()
#etc...
#pag05()

def main():

    root=Tk()
    myGuiPag00=Pag00(root)
    root.mainloop()

if __name__ == '__main__':
    main()

Thanks a lot in advance

Recommended Answers

All 10 Replies

grid() returns None so self.label1 equals None in the code you posted.

There are two ways to update a label, illustrated below. There is way too much code here for me to traverse so the following is independent of any code you posted.

class UpdateLabel():
    def __init__(self):
        self.master = tk.Tk()
        self.label1_text = tk.StringVar()
        self.label1_text.set("initial value")
        self.label1=tk.Label(self.master, textvariable=self.label1_text,
                            fg='blue', font=("Arial", 36, "bold"),
                            background='#CDC5D9')
        self.label1.grid(row=0,column=0)

        self.master.grid_columnconfigure(1, minsize=100)

        self.label2=tk.Label(self.master,text='Initial value',
                            fg='blue', font=("Arial", 36, "bold"),
                            background='#CDC5D9')
        self.label2.grid(row=0,column=2)

        tk.Button(self.master, text="Quit", command=self.master.quit,
                  bg="red").grid(row=1, column=0)

        ## update the label in two different ways
        self.master.after(2000, self.update_label_1) ## sleep for 2 seconds

        self.master.mainloop()

    def update_label_1(self):
        ## update label1 by changing the StringVar()
        self.label1_text.set("2nd value")
        self.label1.config(bg="yellow")


        ## update label2 by setting the text
        self.label2["text"] = "third value"
        self.label2["fg"] = "green"
        ## this is the same as the above line
        ##self.label2.config(text="third value")

UpdateLabel()
commented: good help +14

Thanks for the answer but i haven t managed to work.
I simplified my code to be more easyly to read or test

What i did was to rename your "UpdateLabel" class into "Pag01"

So, the simplified code now, when is run it open the main window and when i press the button for open the pag1, where actually i want my label updated i get this error

root@sysdev01:/var/storage/work/teste# python pages2.py
Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1437, in __call__
    return self.func(*args)
  File "pages2.py", line 33, in gotoPag01
    myGui=Pag01(root1)
TypeError: __init__() takes exactly 1 argument (2 given)
root@sysdev01:/var/storage/work/teste#

The simplified code is this and can be run anywhere, because i dropped the related db or other hardware dependincies

import Tkinter
import tkMessageBox
from Tkinter import *
import MySQLdb
import sys
import datetime
from datetime import timedelta
import time
import calendar
import u3
import struct
import traceback
import uuid
import random

#first page - main
class Pag00():

    def __init__(self,master):

        self.master=master
        self.master.geometry('1024x680+50+50')
        self.master.title('title ')
        self.master.configure(background='#CDC5D9')

        self.label1=Label(self.master,text='test code',fg='blue', font=("Arial", 36, "bold"),background='#CDC5D9').grid(row=0,column=2)

        self.button1=Button(self.master,text='open pag1',fg='blue',font=('Helvetica',26),command=self.gotoPag01,height=1, width=15).grid(row=2,column=2)
        self.button6=Button(self.master,text='Exit',fg='blue',font=('Helvetica',26),command=self.exit,height=1, width=15).grid(row=12,column=2)

    def gotoPag01(self):
        root1=Toplevel(self.master)
        myGui=Pag01(root1)

    def exit(self):
        mExit = tkMessageBox.askyesno(title='Exit', message='Are you sure ?')
        if mExit > 0:
            self.master.destroy()
            return

##################
class Pag01():
    def __init__(self):
        self.master = master
        self.label1_text = tk.StringVar()
        self.label1_text.set("initial value")
        self.label1=tk.Label(self.master, textvariable=self.label1_text,
                            fg='blue', font=("Arial", 36, "bold"),
                            background='#CDC5D9')
        self.label1.grid(row=0,column=0)
        self.master.grid_columnconfigure(1, minsize=100)
        self.label2=tk.Label(self.master,text='Initial value',
                            fg='blue', font=("Arial", 36, "bold"),
                            background='#CDC5D9')
        self.label2.grid(row=0,column=2)
        tk.Button(self.master, text="Quit", command=self.master.quit,
                  bg="red").grid(row=1, column=0)
        ## update the label in two different ways
        self.master.after(2000, self.update_label_1) ## sleep for 2 seconds
        self.master.mainloop()
    def update_label_1(self):
        ## update label1 by changing the StringVar()
        self.label1_text.set("2nd value")
        self.label1.config(bg="yellow")
        ## update label2 by setting the text
        self.label2["text"] = "third value"
        self.label2["fg"] = "green"
        ## this is the same as the above line
        self.label2.config(text="third value")

def main():
    root=Tk()
    myGuiPag00=Pag00(root)
    root.mainloop()

if __name__ == '__main__':
    main()

Again many thanks

Replace the beginning of Pag01 with

class Pag01():
    def __init__(self, master):
        tk = Tkinter
        self.master = master
        self.label1_text = ...
        ...

Thanks this worked.
But actually it update it only once. If i change the value the label is not updated anymore.

this is pag01 code

class Pag01():
    def __init__(self,master):
        tk = Tkinter
        self.master = master
        self.label1_text = tk.StringVar()
        self.label1_text.set("initial value")
        self.label1=tk.Label(self.master, textvariable=self.label1_text,
                            fg='blue', font=("Arial", 36, "bold"),
                            background='#CDC5D9')
        self.label1.grid(row=0,column=0)
        self.master.grid_columnconfigure(1, minsize=100)
        self.label2=tk.Label(self.master,text='Initial value',
                            fg='blue', font=("Arial", 36, "bold"),
                            background='#CDC5D9')
        self.label2.grid(row=0,column=2)
        tk.Button(self.master, text="Quit", command=self.master.quit,
                  bg="red").grid(row=1, column=0)
        ## update the label in two different ways
        self.master.after(500, self.update_label_1) ## sleep for 2 seconds
        self.master.mainloop()
    def update_label_1(self):
        ## update label1 by changing the StringVar()
        ainVoltage[0] = d.getAIN(0, negChannel = 32)
        ain0 = "%.2f" % round(ainVoltage[0],2)
        ain0 = str(ain0)

        self.label1_text.set(ain0)
        self.label1.config(bg="yellow")

When i open page the value is changed with ain0 value from that moment and then it stays on it. If i change ain0 value the label is not updating anymore.
What i am doing wrong?

p.s. I checked ain0 changing value by printing it in a loop and is changing, but the label don t

thank you a lot again

You only call update_label_1 one time so the label is only updated once. This updates the label every time the button is pushed.

class UpdateLabel():
    def __init__(self):
        self.master = tk.Tk()
        self.update_ctr = 0
        self.label1_text = tk.StringVar()
        self.label1_text.set("initial value")
        self.label1=tk.Label(self.master, textvariable=self.label1_text,
                            fg='blue', font=("Arial", 36, "bold"),
                            background='#CDC5D9')
        self.label1.grid(row=0,column=0)

        self.master.grid_columnconfigure(1, minsize=100)

        self.label2=tk.Label(self.master,text='Initial value',
                            fg='blue', font=("Arial", 36, "bold"),
                            background='#CDC5D9')
        self.label2.grid(row=0,column=2)

        tk.Button(self.master, text="Quit", command=self.master.quit,
                  bg="red").grid(row=10, column=0, columnspan=3)

        tk.Button(self.master, text="Update Label",
                  command=self.update_label_1).grid(row=5, column=0)

        self.master.mainloop()

    def update_label_1(self):
        ## update label1 by changing the StringVar()
        self.update_ctr += 1
        self.label1_text.set("update #%d" % (self.update_ctr))
        self.label1.config(bg="yellow")

UpdateLabel()

And if you want better answers you will have to ask better questions. You have said nothing about updating the label multiple times or anything about the conditions under which you want to update it, so we can't do any more than has already been done based upon the question asked.

Ok,
I appologize for this.
By dynamically i though i say what i want but i was wrong.
So, i want that label to be updated 10 times per second with ain0 value, without press a buton or do some action. Just update itself when the window is open.

Thank you again

Or is necessary to open another question topic for this speciffic ?

I tried to use after but without success

class Pag01():
    def __init__(self,master):
        tk = Tkinter
        self.master = master
        self.label1_text = tk.StringVar()
        self.label1_text.set("initial value")
        self.label1=tk.Label(self.master, textvariable=self.label1_text,
                            fg='blue', font=("Arial", 36, "bold"),
                            background='#CDC5D9')
        self.label1.grid(row=0,column=0)
        self.master.grid_columnconfigure(1, minsize=100)
        self.label2=tk.Label(self.master,text='Initial value',
                            fg='blue', font=("Arial", 36, "bold"),
                            background='#CDC5D9')
        self.label2.grid(row=0,column=2)
        tk.Button(self.master, text="Quit", command=self.master.quit,
                  bg="red").grid(row=1, column=0)
        ## update the label in two different ways
        self.master.after(500, self.update_label_1) ## sleep for 2 seconds
        self.master.mainloop()
    def update_label_1(self):
        ## update label1 by changing the StringVar()
        ainVoltage[0] = d.getAIN(0, negChannel = 32)
        ain0 = "%.2f" % round(ainVoltage[0],2)
        ain0 = str(ain0)
        self.label1_text.set(ain0)
        self.label1.config(bg="yellow")
        self.after(1000, self.update_label_1)
        print ain0
        return

see line 28

when i open page i get

Exception in Tkinter callback
Traceback (most recent call last):
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1437, in __call__
    return self.func(*args)
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 498, in callit
    func(*args)
  File "pages4.py", line 236, in update_label_1
    self.after(1000, self.update_label_1)
AttributeError: Pag01 instance has no attribute 'after'

Again me

i think i managed to make it work
i used thread

class Pag01():
    def __init__(self,master):
        tk = Tkinter
        self.master = master
        self.label1_text = tk.StringVar()
        self.label1_text.set("initial value")
        self.label1=tk.Label(self.master, textvariable=self.label1_text,
                            fg='blue', font=("Arial", 36, "bold"),
                            background='#CDC5D9')
        self.label1.grid(row=0,column=0)
        self.master.grid_columnconfigure(1, minsize=100)
        self.label2=tk.Label(self.master,text='Initial value',
                            fg='blue', font=("Arial", 36, "bold"),
                            background='#CDC5D9')
        self.label2.grid(row=0,column=2)
        tk.Button(self.master, text="Quit", command=self.master.quit,
                  bg="red").grid(row=1, column=0)
        ## update the label in two different ways
        self.master.after(500, self.update_label_1) ## sleep for 2 seconds
        self.master.mainloop()
    def update_label_1(self):
        ## update label1 by changing the StringVar()
        ainVoltage[0] = d.getAIN(0, negChannel = 32)
        ain0 = "%.2f" % round(ainVoltage[0],2)
        ain0 = str(ain0)
        self.label1_text.set(ain0)
        self.label1.config(bg="yellow")
        threading.Timer(0.5, self.update_label_1).start()
        print ain0
        return

seems ok at the forst side

thanks a lot for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.