Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Unable to update or refresh label text in tkinter
#1
In class Window2 I am trying to update the label text by taking the data from a variable which is showing the real-time data but I am not able to refresh my label text using below code:


import tkinter as tk
from tkinter import *
import tkinter.messagebox
from tkinter import ttk
import random
import time
import datetime
import imutils
from PIL import ImageTk, Image

def main():
    root = Tk()
    app = Window1(root)
    root.mainloop()
    return

class Window1:
    def __init__(self,master):
        self.master = master
        self.master.title("Test Login System")
        self.master.geometry('1350x750+0+0')
        self.master.config(bg='powder blue')
        self.frame = Frame(self.master, bg ='powder blue')
        self.frame.pack()

        self.username = StringVar()
        self.password = StringVar()
        self.lblTitle = Label(self.frame,  text = 'Test Login System', font = ('arial',30,'bold'), bg='powder blue',
                              fg = 'black')
        self.lblTitle.grid(row=0, columnspan = 2, pady = 40)

        #==================================================================================================

        self.LoginFrame1 = LabelFrame(self.frame, width = 1350, height = 600
                                ,font = ('arial',20,'bold'), relief = 'ridge', bg = 'cadet blue', bd = 20)
        self.LoginFrame1.grid(row = 1, column =0)

        self.LoginFrame2 = LabelFrame(self.frame, width=1000, height=600
                                ,font=('arial', 20, 'bold'), relief='ridge', bg='cadet blue', bd = 20)
        self.LoginFrame2.grid(row=2, column=0)

        #=====================================Label and Entry==============================================

        self.lblUsername = Label(self.LoginFrame1,text = 'Username', font=('arial', 10, 'bold'), bd = 22,
                                 bg= 'cadet blue', fg = 'Cornsilk')
        self.lblUsername.grid(row=0, column=0)
        self.txtUsername = Entry(self.LoginFrame1, font = ('arial', 15, 'bold'),textvariable= self.username)
        self.txtUsername.grid(row=0, column=1, padx =8, pady =8)

        self.lblpassword = Label(self.LoginFrame1,text = 'Password', font=('arial', 10, 'bold'),bd = 22,
                                 bg= 'cadet blue', fg = 'Cornsilk')
        self.lblpassword.grid(row=1, column=0, padx =8, pady =8)
        self.txtpassword = Entry(self.LoginFrame1, font = ('arial', 15, 'bold'), textvariable= self.password)
        self.txtpassword.grid(row=1, column=1)

        #=====================================Button========================================================
        self.btnLogin = Button(self.LoginFrame2, text='Login', width=17, font = ('arial', 8), command = self.Login_System)
        self.btnLogin.grid(row=0, column=1, padx =10, pady =10)

        self.btnReset = Button(self.LoginFrame2, text='Reset', width=17, font = ('arial', 8), command = self.Rest)
        self.btnReset.grid(row=0, column=2, padx =10, pady =10)

        self.btnExit = Button(self.LoginFrame2, text='Exit', width=17, font = ('arial', 8), command = self.iExit)
        self.btnExit.grid(row=0, column=3, padx =10, pady =10)

    def Login_System(self):
        u = (self.username.get())
        p = (self.password.get())

        if (u == str("admin") and p == str("admin")):
            self.newWindow = Toplevel(self.master)
            self.app = Window2(self.newWindow)

        else:
            tkinter.messagebox.askyesno("Login System","Invalid Login Detail")
            self.username.set("")
            self.password.set("")
            self.txtUsername.focus()

    def Rest(self):
        self.username.set("")
        self.password.set("")
        self.txtUsername.focus()

    def iExit(self):
        self.iExit = tkinter.messagebox.askyesno("Login System", "Are You Sure you want to Exit?")
        if self.iExit > 0:
            self.master.destroy()
        else:
            command = self.new_window
            return


    def new_window(self):
        self.newWindow = Toplevel(self.master)
        self.app = Window2(self.newWindow)


class Window2:

    def loopCap(self):
        self.img = Image.open('img.jpg')
        self.label = ImageTk.PhotoImage(self.img)
        self.canvas.create_image(0, 0, anchor=NW, image=self.label)
        self.canvas.after(100, self.loopCap)
        print("updated")

    def lbl_txt(self):
        self.text = StringVar()
        self.text.set("99.97")
        self.label.config(text=str(self.text))
        print("Text Updated")
        self.label.after(100, self.lbl_txt)


    def __init__(self, master):
        self.master = master
        self.master.title("Test Reference System")
        self.master.geometry('1350x750+0+0')
        self.master.config(bg='powder blue')
        self.frame = Frame(self.master, bg='powder blue')
        self.frame.pack()

        self.lblTitle = Label(self.frame, text='Detection Image', font=('arial', 30, 'bold'), bg='powder blue',
                              fg='black',anchor=CENTER)
        self.lblTitle.grid(row=0, columnspan=3, pady=20)


        self.lblTitle1 = Label(self.frame, text='Accuracy: ', font=('arial', 15, 'bold'), bd=1, bg='powder blue',
                              fg='black', anchor=W)
        self.lblTitle1.grid(row=1, pady=10)

        self.text = StringVar()
        self.text.set("99.97")
        self.lblTitle2 = Label(self.frame, textvariable=self.lbl_txt, font=('arial', 15, 'bold'), bd=1, bg='powder blue',
                               fg='black', anchor=W)
        self.lblTitle2.grid(row=1, column=1, pady=5)


        self.canvas = Canvas(self.master, width =700, height = 700)
        self.canvas.pack()
        self.img = Image.open('img.jpg')
        self.label = ImageTk.PhotoImage(self.img)
        self.canvas.create_image(0,0, anchor=CENTER, image=self.label)
        print("Load")
        self.canvas.after(100, self.loopCap)


if __name__ == '__main__':
    main()
Suggestions will be very helpful here
Quote
#2
Shouldn't be sth like this :
img = Image.open('img.jpg')
self.img = ImageTk.PhotoImage(img)
self.label = Label(master, image=self.img, textvariable=self.text)
Quote
#3
do I have to make some change in image update code?
Quote
#4
i am not sure if i understood you, but if my first proposal is ok, then this should be changed too:
self.canvas.create_image(0, 0, anchor=NW, image=self.img)
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Active tkinter text output during loop dvanommen 2 229 Oct-18-2019, 02:23 PM
Last Post: dvanommen
  Update a label text from subprocess jim53 3 402 Aug-19-2019, 08:21 PM
Last Post: Denni
  [Kivy] [CRITICAL] [App ] Unable to get a Text provider, abort. Exsul 6 2,127 Aug-17-2019, 11:54 PM
Last Post: Exsul
  Spacing Between two labels are very far and Top label is not in Center using Tkinter barry76 2 250 Jul-30-2019, 10:49 AM
Last Post: wuf
  Refresh image in label after every 1s using simple function jenkins43 1 267 Jul-28-2019, 02:49 PM
Last Post: Larz60+
  [Tkinter] Line Number Tkinter Text Are smabubakkar 1 381 Jun-22-2019, 07:05 PM
Last Post: woooee
  Update value selected in option menu when select an item from text box klllmmm 2 659 Jun-06-2019, 04:51 AM
Last Post: klllmmm
  [Tkinter] Having trouble updating the text in a label microphone_head 3 452 May-02-2019, 08:44 AM
Last Post: microphone_head
  how to insert image into Text widget Tkinter atlass218 5 1,638 Apr-17-2019, 05:28 AM
Last Post: atlass218
  Tkinter - Make changes to graph and update it adriancovaci 0 925 Apr-08-2019, 09:02 AM
Last Post: adriancovaci

Forum Jump:


Users browsing this thread: 1 Guest(s)