#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2015
    Posts
    2
    Rep Power
    0

    Question [Tkinter] TypeError: 'int' object does not support item assignment


    Hello!

    To kick this off, i am a complete noob at programming in general; and i learn best by DOING rather than reading.

    I am writing a basic program with Python using a GUI through VB.

    I have only been using python for a few hours, so i don't have a great understanding of the basic strings.

    Can someone point out why i am getting this error?

    (TypeError: 'int' object does not support item assignment)

    My code is below. There are three buttons that do nothing, then a fourth that will serve as my counter.
    All i wish to do is make the button count how many times i click it.

    I am putting notes around, so that i can remind myself what im doing.
    Code:
    #Creating a GUI using a Class string
    
    from tkinter import *
    
    class Application(Frame):
    #Initiate the Frame
    	def __init__(self, master):
    		Frame.__init__(self, master)
    		self.grid()
    		self.button_clicks =0 #Counting number of button clicks
    		self.create_widgets()
    
    #Create a button
    	def create_widgets(self):
    		self.button1 = Button(self, text = "This is a button")
    		self.button1.grid()
    
    		self.button2 = Button(self)
    		self.button2.grid()
    		self.button2.configure(text = "This is another button")
    
    		self.button3 = Button(self)
    		self.button3.grid()
    		self.button3["text"] = "This is a third button"
    	
    	  #Creating a button to count clicks
    		self.button4 = Button (self)
    		self.button4["text"] = "Total Clicks : 0"
    		self.button4["command"] = self.update_count
    		self.button4.grid()
    
    	   #Defining update_count
    	def update_count(self):
    		self.button_clicks += 1
    		self.button_clicks["text"] = "Total Clicks : " + str(self.button_clicks)
    
    
    root = Tk()
    root.title("This is a GUI")
    root.geometry("200x100")
    
    app = Application(root)
    
    root.mainloop()
    Any help or pointers is much appreciated! I plan to end up (Not basing it on this program) to create a GUI for a set of .py scripts that someone else already has, in order to be usable on a small Raspberry Pi based device with a touchscreen. Any pointers to the best way to incorporate other .py scripts would also be very helpful!

    Thanks guys!
  2. #2
  3. Contributing User

    Join Date
    Aug 2011
    Posts
    5,306
    Rep Power
    505
    you'd need to change the text attribute of the button. See https://docs.python.org/3/tutorial/index.html
    Code:
        def update_count(self):
            self.button_clicks += 1
            self.button4["text"] = "Total Clicks : " + str(self.button_clicks)

    Comments on this post

    • Rob95 agrees
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2015
    Posts
    2
    Rep Power
    0
    Originally Posted by b49P23TIvg
    you'd need to change the text attribute of the button. See https://docs.python.org/3/tutorial/index.html
    Code:
        def update_count(self):
            self.button_clicks += 1
            self.button4["text"] = "Total Clicks : " + str(self.button_clicks)
    BAH! Just a silly mistake. Thanks for pointing that out. I cant believe i re wrote that like 5 times without thinking that would be a problem. I suppose the df is going to magically 'know' which button to change the text on.

    Thanks!

IMN logo majestic logo threadwatch logo seochat tools logo