Tushar Saxena
Tushar Saxena

Reputation: 11

Tkinter - No attribute Label 'get()' error

I am making a counter application so like there are 2 teams red and blue. It's like if you are playing a game of rock paper scissors you want to keep a count of the points you use this.

So I am trying to use the get() with a label and it show no atribute and also the red team counter is not displaying.

from tkinter import *

#Creating a  new instance
root = Tk()

#Icon for the app
root.iconbitmap('D:\Documents\ico\counter.ico')

# setting up title for the program
root.title("Counter")

#Creating functions for all the instances

def blue_counter_update():
    f_num = int(blue_label_number.get())
    blue_label_number.delete(0,END)
    blue_label_number.insert(0,f_num+1)


def red_label_color():
    red_label.config(bg="red")

def blue_label_color():
    blue_label.config(bg="blue")

#creating a label for the counter
red_label_head = Label(root , text = "RED TEAM",fg = "red")
blue_label_head = Label(root , text = "BLUE TEAM",fg="blue")
red_label = Label(root , text = "Counter πŸ‘‡πŸΌ")
red_label_number = Label(root , text = "0")
blue_label_number = Label(root , text = "0")
blue_label = Label(root , text = "Counter πŸ‘‡πŸΌ ")
counter_header = Label(root , text = "COUNTER")

#Creating a button for the counter
counter_header.grid(row = 0 , column =0 , columnspan =3)
red_button = Button(root , text = "Increase Counter")
blue_button = Button(root , text = "Increase Counter",command =blue_counter_update)

# griding all the instances
red_label_head.grid(row = 1 , column = 1)
red_label.grid(row = 2 , column = 1)
red_label_number.grid(row = 4 , column = 1)
red_button.grid(row = 5 , column = 1)

blue_label_head.grid(row = 1 , column = 2)
blue_label.grid(row = 2 , column = 2)
red_label_number.grid(row = 4 , column = 2)
blue_button.grid(row = 5 , column = 2)

#Setting the size of the tkinter instance
root.geometry("200x110")

#Making the instance de-sizeable
root.resizable(False, False)

#Looping the Tkinter instance
root.mainloop()

Upvotes: 0

Views: 61

Answers (2)

noushin
noushin

Reputation: 9

you can get text of Label by cget for example

  f_num = int(blue_label_number.cget("text"))

and also we have not have delete and insert for labeΩ… so i suggest you use a variable to increase it for each button press and then add it to text of label by using label.config

def blue_counter_update():
  f_num= int(blue_label_number.cget("text"))
  f_num +=1
  blue_label_number.config(text= str(f_num) )

Upvotes: 0

Tkinter Lover
Tkinter Lover

Reputation: 855

This is not the way to get the text of a label. Instead, do label.cget("text")

Upvotes: 1

Related Questions