Python Forum
[Tkinter] Having trouble updating the text in a label
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Having trouble updating the text in a label
#1
Hi all,

Having trouble updating the text in a label Wall .

I've created a tkinter window and created an update routine, but its not obvious to myself as to how to set the attributes Huh . Any suggestions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import tkinter as tk
import pygame
 
pygame.init()
 
pwbr_power = 0
pwbr_duration = 0
pwbr_scrn_refresh = 0
pwbr_elps_time = 0
pwbr_ttl_sec = 0
 
class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.grid()
        self.createwidgets()
    # __init__() ---------------------------------------------------------------
 
    def createwidgets(self):
        """
        Creates the numerous widgets that make up the form.
        """
         
        global pwbr_power, pwbr_duration, pwbr_scrn_refresh, pwbr_elps_time
        global pwbr_ttl_sec
         
        pwbr_power += 1
        pwbr_duration += 0.15
        pwbr_elps_time += pwbr_duration
        pwbr_scrn_refresh = 0.333
        pwbr_ttl_sec += pwbr_elps_time
 
        if pwbr_elps_time > pwbr_scrn_refresh:
            pwbr_elps_time = 0
            pwbr_duration = 0
 
        caption_pwr = "Power: " + str(pwbr_power)
        caption_dur = "Duration: " + str(pwbr_duration)
        caption_rfrsh =  "Screen Refresh: " + str(pwbr_scrn_refresh)
        caption_etime = "Elapsed time: " + str(pwbr_elps_time)
        caption_ttl = "Total sec's: " + str(pwbr_ttl_sec)
         
        self.lblpower = tk.Label(self, text=caption_pwr, fg="black")
        self.lblduration = tk.Label(self, text=caption_dur, fg="black")
        self.lblrefreshrate = tk.Label(self, text=caption_rfrsh, fg="black")
        self.lbletime = tk.Label(self, text=caption_etime, fg="black")
        self.lbltotalsec = tk.Label(self, text=caption_ttl, fg="black")
         
        self.quitButton = tk.Button(self, text='Quit', command=self.quit)
         
        self.lblpower.grid()
        self.lblduration.grid()
        self.lblrefreshrate.grid()
        self.lbletime.grid()
        self.lbltotalsec.grid()
        self.quitButton.grid()
         
        self.after(1000, self.update)
    # createwidgets() ----------------------------------------------------------
     
 
    def update(self):
        """
        Updates the status information in the tkinter window.
        """
         
        global pwbr_power, pwbr_duration, pwbr_scrn_refresh, pwbr_elps_time
        global pwbr_ttl_sec
         
        pwbr_power += 1
        pwbr_duration += 0.15
        pwbr_elps_time += pwbr_duration
        pwbr_scrn_refresh = 0.333
        pwbr_ttl_sec += pwbr_elps_time
 
        if pwbr_elps_time > pwbr_scrn_refresh:
            pwbr_elps_time = 0
            pwbr_duration = 0
 
        caption_pwr = "Power: " + str(pwbr_power)
        caption_dur = "Duration: " + str(pwbr_duration)
        caption_rfrsh =  "Screen Refresh: " + str(pwbr_scrn_refresh)
        caption_etime = "Elapsed time: " + str(pwbr_elps_time)
        caption_ttl = "Total sec's: " + str(pwbr_ttl_sec)
 
        self.lblpower.text = "1234"
##        self.lblduration = tk.Label(self, text=caption_dur, fg="black")
##        self.lblrefreshrate = tk.Label(self, text=caption_rfrsh, fg="black")
##        self.lbletime = tk.Label(self, text=caption_etime, fg="black")
##        self.lbltotalsec = tk.Label(self, text=caption_ttl, fg="black")
##       
##        self.quitButton = tk.Button(self, text='Quit', command=self.quit)
 
        self.after(1000, self.update)
    # fn_pbtkwin_update() ----------------------------------------------------------
# Application() ----------------------------------------------------------------
 
 
app = Application()
app.master.title("Power Bar")
app.mainloop()
 
 
pygame.time.wait(1000)
pygame.display.set_mode((640, 480))
 
quit_loop = False
while quit_loop is not True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:  # If user clicked close
            quit_loop = True  # Flag that we are done so we exit this loop
        elif event.type == pygame.MOUSEBUTTONUP:
            pass
        elif event.type == pygame.constants.KEYDOWN:
            if event.key == pygame.constants.K_ESCAPE:
                quit_loop = True
                break
            elif event.key == pygame.constants.K_LEFT:
                kbd["left"] = True
            elif event.key == pygame.constants.K_RIGHT:
                kbd["right"] = True
            elif event.key == pygame.constants.K_UP:
                kbd["up"] = True
            elif event.key == pygame.constants.K_DOWN:
                kbd["down"] = True
            elif event.key == pygame.constants.K_SPACE:  # Fire.
                kbd["fire"] = True
            elif event.key == pygame.constants.K_LCTRL:  # Thrust.
                kbd["thrust"] = True
            elif event.key == pygame.constants.K_r:
                kbd["overload"] = True
        elif event.type == pygame.constants.KEYUP:
            if event.key == pygame.constants.K_LEFT:
                kbd["left"] = False
            elif event.key == pygame.constants.K_RIGHT:
                kbd["right"] = False
            elif event.key == pygame.constants.K_UP:
                kbd["up"] = False
            elif event.key == pygame.constants.K_DOWN:
                kbd["down"] = False
            elif event.key == pygame.constants.K_SPACE:  # Fire.
                kbd["fire"] = False
            elif event.key == pygame.constants.K_LCTRL:  # Thrust.
                kbd["thrust"] = False
            elif event.key == pygame.constants.K_r:
                kbd["overload"] = False
            # end if
        # end if
    # end for loop
 
    # { Paint some graphics on the screen.
    #   ...
    # }.
    pygame.display.flip()
# end while loop

That was brutal Cry . Took a while but I got there in the end. Turns out they use a config method. Had a few passed at skimming the reference document Tkinter 8.5 and didn't see it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import tkinter as tk
import pygame
 
pygame.init()
 
pwbr_power = 0
pwbr_duration = 0
pwbr_scrn_refresh = 0
pwbr_elps_time = 0
pwbr_ttl_sec = 0
 
class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.grid()
        self.createwidgets()
    # __init__() ---------------------------------------------------------------
 
    def createwidgets(self):
        """
        Creates the numerous widgets that make up the form.
        """
         
        global pwbr_power, pwbr_duration, pwbr_scrn_refresh, pwbr_elps_time
        global pwbr_ttl_sec
         
        pwbr_power += 1
        pwbr_duration += 0.15
        pwbr_elps_time += pwbr_duration
        pwbr_scrn_refresh = 0.333
        pwbr_ttl_sec += pwbr_elps_time
 
        if pwbr_elps_time > pwbr_scrn_refresh:
            pwbr_elps_time = 0
            pwbr_duration = 0
 
        caption_pwr = "Power: " + str(pwbr_power)
        caption_dur = "Duration: " + str(pwbr_duration)
        caption_rfrsh =  "Screen Refresh: " + str(pwbr_scrn_refresh)
        caption_etime = "Elapsed time: " + str(pwbr_elps_time)
        caption_ttl = "Total sec's: " + str(pwbr_ttl_sec)
         
        self.lblpower = tk.Label(self, text=caption_pwr, fg="black")
        self.lblduration = tk.Label(self, text=caption_dur, fg="black")
        self.lblrefreshrate = tk.Label(self, text=caption_rfrsh, fg="black")
        self.lbletime = tk.Label(self, text=caption_etime, fg="black")
        self.lbltotalsec = tk.Label(self, text=caption_ttl, fg="black")
         
        self.quitButton = tk.Button(self, text='Quit', command=self.quit)
         
        self.lblpower.grid()
        self.lblduration.grid()
        self.lblrefreshrate.grid()
        self.lbletime.grid()
        self.lbltotalsec.grid()
        self.quitButton.grid()
        self.update()
         
        self.after(1000, self.update_info)
    # createwidgets() ----------------------------------------------------------
     
 
    def update_info(self):
        """
        Updates the status information in the tkinter window.
        """
         
        global pwbr_power, pwbr_duration, pwbr_scrn_refresh, pwbr_elps_time
        global pwbr_ttl_sec
         
        pwbr_power += 1
        pwbr_duration += 0.15
        pwbr_elps_time += pwbr_duration
        pwbr_scrn_refresh = 0.333
        pwbr_ttl_sec += pwbr_elps_time
 
        if pwbr_elps_time > pwbr_scrn_refresh:
            pwbr_elps_time = 0
            pwbr_duration = 0
 
        caption_pwr = "Power: " + str(pwbr_power)
        caption_dur = "Duration: " + str(pwbr_duration)
        caption_rfrsh =  "Screen Refresh: " + str(pwbr_scrn_refresh)
        caption_etime = "Elapsed time: " + str(pwbr_elps_time)
        caption_ttl = "Total sec's: " + str(pwbr_ttl_sec)
 
        self.lblpower.config(text=caption_pwr)
        self.lblduration.config(text=caption_dur)
        self.lblrefreshrate.config(text=caption_rfrsh)
        self.lbletime.config(text=caption_etime)
        self.lbltotalsec.config(text=caption_ttl)
 
        self.after(1000, self.update_info)
    # update_info() ----------------------------------------------------------
# Application() ----------------------------------------------------------------
 
 
pygame.display.set_mode((640, 480))
 
app = Application()
app.master.title("Power Bar")
app.mainloop()
 
 
 
quit_loop = False
while quit_loop is not True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:  # If user clicked close
            quit_loop = True  # Flag that we are done so we exit this loop
        elif event.type == pygame.MOUSEBUTTONUP:
            pass
        elif event.type == pygame.constants.KEYDOWN:
            if event.key == pygame.constants.K_ESCAPE:
                quit_loop = True
                break
            elif event.key == pygame.constants.K_LEFT:
                kbd["left"] = True
            elif event.key == pygame.constants.K_RIGHT:
                kbd["right"] = True
            elif event.key == pygame.constants.K_UP:
                kbd["up"] = True
            elif event.key == pygame.constants.K_DOWN:
                kbd["down"] = True
            elif event.key == pygame.constants.K_SPACE:  # Fire.
                kbd["fire"] = True
            elif event.key == pygame.constants.K_LCTRL:  # Thrust.
                kbd["thrust"] = True
            elif event.key == pygame.constants.K_r:
                kbd["overload"] = True
        elif event.type == pygame.constants.KEYUP:
            if event.key == pygame.constants.K_LEFT:
                kbd["left"] = False
            elif event.key == pygame.constants.K_RIGHT:
                kbd["right"] = False
            elif event.key == pygame.constants.K_UP:
                kbd["up"] = False
            elif event.key == pygame.constants.K_DOWN:
                kbd["down"] = False
            elif event.key == pygame.constants.K_SPACE:  # Fire.
                kbd["fire"] = False
            elif event.key == pygame.constants.K_LCTRL:  # Thrust.
                kbd["thrust"] = False
            elif event.key == pygame.constants.K_r:
                kbd["overload"] = False
            # end if
        # end if
    # end for loop
 
    # { Paint some graphics on the screen.
    #   ...
    # }.
    pygame.display.flip()
# end while loop
Reply
#2
which label (to use as example)? you have many
Reply
#3
You can also treat the option name like a dictionary index
1
self.lblpower['text']=caption_pwr
Ref
https://docs.python.org/3/library/tkinte...-reference
https://docs.python.org/3/library/tkinte...el-options
or widget variables
https://docs.python.org/3/library/tkinte...-variables
Reply
#4
(May-01-2019, 07:50 PM)Larz60+ Wrote: which label (to use as example)? you have many

Its seems to be working now, I was refering to the previous post (above) that was was attempting update the text in a label, this was done in the update method of the Appllication class.

1
self.lblpower.text = "any text value"
Instead I found I could achieve the goal by using the config method:
1
self.lblpower.config(text=caption_pwr)

(May-01-2019, 11:24 PM)Yoriz Wrote: You can also treat the option name like a dictionary index
1
self.lblpower['text']=caption_pwr
Ref
https://docs.python.org/3/library/tkinte...-reference
https://docs.python.org/3/library/tkinte...el-options
or widget variables
https://docs.python.org/3/library/tkinte...-variables

Thanks for the web links Smile , the handy reference looks more like my cup of tea.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to update label text using a grid button. Edward_ 7 1,579 Dec-18-2024, 03:05 AM
Last Post: Edward_
  update text variable on label with keypress knoxvilles_joker 5 7,594 May-31-2024, 02:09 PM
Last Post: menator01
  [Tkinter] Updating tkinter text BliepMonster 5 11,769 Nov-28-2022, 01:42 AM
Last Post: deanhystad
  [Tkinter] Updating Tkinter label using multiprocessing Agusms 6 5,633 Aug-15-2022, 07:10 PM
Last Post: menator01
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 10,211 Jun-26-2022, 06:26 PM
Last Post: menator01
  Updating button text based upon different variable values knoxvilles_joker 0 2,866 Apr-18-2021, 04:13 AM
Last Post: knoxvilles_joker
  How to read text in kivy textinput or Label jadel440 1 6,465 Dec-29-2020, 10:47 AM
Last Post: joe_momma
  [Kivy] Kivy text label won't shows up! AVD_01 1 3,805 Jun-21-2020, 04:01 PM
Last Post: AVD_01
  [Tkinter] Python 3 change label text gw1500se 6 6,484 May-08-2020, 05:47 PM
Last Post: deanhystad
  [Tkinter] how to update label text from list Roshan 8 7,348 Apr-25-2020, 08:04 AM
Last Post: Roshan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020