Bottom Page

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
Quote
#2
What's the whole traceback?
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to make button text bold in Tkinter? scratchmyhead 2 109 May-16-2020, 02:53 AM
Last Post: scratchmyhead
  [Tkinter] changing title text to bold in tkinter Kumarkv 2 160 May-09-2020, 10:41 PM
Last Post: Larz60+
  [Tkinter] Python 3 change label text gw1500se 6 685 May-08-2020, 05:47 PM
Last Post: deanhystad
  [Kivy] Kivy text label won't shows up! AVD_01 0 241 May-02-2020, 07:11 AM
Last Post: AVD_01
  [Tkinter] Text Button - How Do I Reduce The Margin? vman44 6 221 Apr-27-2020, 10:48 PM
Last Post: Larz60+
  Problem about image and button scotesse 5 295 Apr-27-2020, 10:09 AM
Last Post: scotesse
  [Tkinter] how to update label text from list Roshan 8 236 Apr-25-2020, 08:04 AM
Last Post: Roshan
  [PyQt] Python PyQt5 - Change label text dynamically based on user Input ppel123 1 435 Mar-20-2020, 07:21 AM
Last Post: deanhystad
  Anytime I press the button, the result is depicted Jionni 2 219 Feb-24-2020, 10:08 AM
Last Post: Jionni
  Problem with Submit button Tkinter Reldaing 2 308 Jan-05-2020, 01:58 AM
Last Post: balenaucigasa

Forum Jump:


Users browsing this thread: 1 Guest(s)