Python Forum
[Tkinter] Problem with changing label text on button press
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Problem with changing label text on button press
#1
Hi,

I'm trying to change the label text on button press, but I get an error:
Error:
App object has no attribute saved_length1
In the GUI there are 3 SAVE Buttons, all of them should save one position with three coordinates (length, rotation, lifting). There are also 3 rows at the bottom of the window, which should display the saved positions (it's a 3x3 table from labels + headings). The elements of the appropriate row of the table (set 0 as default) should be changed by clicking on any of the SAVE buttons. There are some labels, buttons & co which I deleted for this post, because everything else works except changing the labels in that table.

Here's my code:

from tkinter import *
import serial
import threading
import sys

length_pos1 = 0
length_pos2 = 0
length_pos3 = 0
angle_pos1 = 0
angle_pos2 = 0
angle_pos3 = 0
lift_pos1 = 0
lift_pos2 = 0
lift_pos3 = 0

class App(threading.Thread):

   def __init__(self, ser):
       threading.Thread.__init__(self)
       self.ser = ser
       self.start()

   def callback(self):
       self.root.quit()

   def save(self, number):
       global length_pos1
       global length_pos2
       global length_pos3
       global angle_pos1
       global angle_pos2
       global angle_pos3
       global lift_pos1
       global lift_pos2
       global lift_pos3

       if( number == 1):
           length_pos1 = self.Length_entry.get()
           angle_pos1  = self.Rotation_entry.get()
           lift_pos1   = self.Lift_entry.get()
           self.display_saved_pos(1)
       elif (number == 2):
           length_pos2 = self.Length_entry.get()
           angle_pos2  = self.Rotation_entry.get()
           lift_pos2   = self.Lift_entry.get()
           self.display_saved_pos(2)
       elif (number == 3):
           length_pos3 = self.Length_entry.get()
           angle_pos3  = self.Rotation_entry.get()
           lift_pos3   = self.Lift_entry.get()
           self.display_saved_pos(3)

   def display_saved_pos(self, number):
       if (number == 1):
           self.saved_length1.config (text = str(length_pos1))
           self.saved_angle1.config (text = str(angle_pos1))
           self.saved_lift1.config (text = str(lift_pos1))
       elif (number == 2):
           self.saved_length2.config (text = str(length_pos2))
           self.saved_angle2.config (text = str(angle_pos2))
           self.saved_lift2.config (text = str(lift_pos2))
       elif (number == 3):
           self.saved_length3.config (text = str(length_pos3))
           self.saved_angle3.config (text = str(angle_pos3))
           self.saved_lift3.config (text = str(lift_pos3))

   def run(self):
       self.root =  Tk()
       self.root.protocol("WM_DELETE_WINDOW", self.callback)

       self.Save1_btn = Button(self.root, text = "SAVE", command = self.save(1))
       self.Save2_btn = Button(self.root, text = "SAVE", command = self.save(2))
       self.Save3_btn = Button(self.root, text = "SAVE", command = self.save(3))

       self.saved_length1 = Label(self.root, text = "0")
       self.saved_length2 = Label(self.root, text = "0")
       self.saved_length3 = Label(self.root, text = "0")
       self.saved_angle1  = Label(self.root, text = "0")
       self.saved_angle2  = Label(self.root, text = "0")
       self.saved_angle3  = Label(self.root, text = "0")
       self.saved_lift1   = Label(self.root, text = "0")
       self.saved_lift2   = Label(self.root, text = "0")
       self.saved_lift3   = Label(self.root, text = "0")
       self.saved_length1.pack()
       self.saved_length2.pack()
       self.saved_length3.pack()
       self.saved_angle1.pack()
       self.saved_angle2.pack()
       self.saved_angle3.pack()
       self.saved_lift1.pack()
       self.saved_lift1.pack()
       self.saved_lift1.pack()

       self.Length_entry.grid(  row = 3, column = 2)
       self.Rotation_entry.grid(row = 4, column = 2)
       self.Lift_entry.grid(    row = 5, column = 2)

       self.Save1_btn.grid( row = 3, column = 6)
       self.Save2_btn.grid( row = 4, column = 6)
       self.Save3_btn.grid( row = 5, column = 6)
      
       self.saved_length1.grid(row = 7, column = 2)
       self.saved_length2.grid(row = 8, column = 2)
       self.saved_length3.grid(row = 9, column = 2)
       self.saved_angle1.grid(row = 7, column = 3)
       self.saved_angle2.grid(row = 8, column = 3)
       self.saved_angle3.grid(row = 9, column = 3)
       self.saved_lift1.grid(row = 7, column = 4)
       self.saved_lift2.grid(row = 8, column = 4)
       self.saved_lift3.grid(row = 9, column = 4)

       self.root.mainloop()


ser = serial.Serial('COM3', 9600, timeout = 1)
app = App(ser)

while app.is_alive():
   bytes_to_read = ser.inWaiting()
   if bytes_to_read > 0:
       sys.stdout.write(ser.read(bytes_to_read).decode("utf-8"))
       sys.stdout.flush()

app.join()
Thanks for any help.

Chris
Reply
#2
What's the whole traceback?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Updating button text based upon different variable values knoxvilles_joker 0 230 Apr-18-2021, 04:13 AM
Last Post: knoxvilles_joker
  update text variable on label with keypress knoxvilles_joker 3 356 Apr-17-2021, 11:21 PM
Last Post: knoxvilles_joker
  .get() invoke after a button nested press iddon5 5 508 Mar-29-2021, 03:55 AM
Last Post: deanhystad
  tkinter python button position problem Nick_tkinter 3 674 Jan-31-2021, 05:15 AM
Last Post: deanhystad
  How to read text in kivy textinput or Label jadel440 1 563 Dec-29-2020, 10:47 AM
Last Post: joe_momma
  tkinter touchscreen scrolling - button press makes unwanted scrolling nanok66 1 541 Dec-28-2020, 10:00 PM
Last Post: nanok66
  [Tkinter] Button click problem using OOP JohnB 5 652 Oct-21-2020, 12:43 PM
Last Post: JohnB
  tkinter | Button color text on Click Maryan 2 701 Oct-09-2020, 08:56 PM
Last Post: Maryan
  [Tkinter] Trying to change font size w/o changing button size python63 3 4,032 Aug-05-2020, 01:04 AM
Last Post: Larz60+
  [Kivy] Kivy text label won't shows up! AVD_01 1 1,079 Jun-21-2020, 04:01 PM
Last Post: AVD_01

Forum Jump:

User Panel Messages

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