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
  [Tkinter] Text Upload LearningLittlebyLittle 0 121 Sep-04-2020, 07:55 PM
Last Post: LearningLittlebyLittle
  Tkinter: How to assign calculated value to a Label LoneStar 7 206 Sep-03-2020, 08:19 PM
Last Post: LoneStar
  [Kivy] Kivy text label won't shows up! AVD_01 1 495 Jun-21-2020, 04:01 PM
Last Post: AVD_01
  [Tkinter] Indentation for Tkinter-made Text Editor ShakeyPakey 4 409 Jun-08-2020, 03:13 PM
Last Post: menator01
  changing tkinter label from thread nanok66 3 412 Jun-07-2020, 01:37 AM
Last Post: nanok66
  How to make button text bold in Tkinter? scratchmyhead 2 403 May-16-2020, 02:53 AM
Last Post: scratchmyhead
  [Tkinter] changing title text to bold in tkinter Kumarkv 2 493 May-09-2020, 10:41 PM
Last Post: Larz60+
  [Tkinter] Python 3 change label text gw1500se 6 948 May-08-2020, 05:47 PM
Last Post: deanhystad
  [Tkinter] how to update label text from list Roshan 8 560 Apr-25-2020, 08:04 AM
Last Post: Roshan
  [Tkinter] tkinter How to pass label fiilename to another module? johnjh 0 382 Apr-17-2020, 11:34 PM
Last Post: johnjh

Forum Jump:


Users browsing this thread: 1 Guest(s)