pythonvariablestkinterinteger

How to calculate user entered numbers in Tkinter textboxs/entryboxs and output to another textbox, or a label?


I have converted a paper board game into a program as my first project using Python and Tkinter. When the game is over, there are four text boxes into which the player can enter their scores. There is also a fifth text box into which they can enter how many points they have lost for skipping turns.

What I am trying to get to work is when the user has entered all these numbers in the appropriate boxes, they can click on a button titled "Calculate", and the sum of the four scores, minus the points for skipping turns, will be displayed in a label, or another textbox.

I'm very new to programming and I'm struggling to get this to work.

I've attached a picture of the program to help with understanding what I'm trying to do.

The game, note the score boxes at the bottom

I have been practicing outside my actual game program trying to add the user-entered numbers in two entry boxes together, but it hasn't worked. I know what I want the code to do in plain English: "Take all these integer variables that the user enters in these textboxes, perform the calculation, and display the result here." However, I don't have the knowledge of Python syntax to achieve this. And I couldn't find anything online.

I will include my practice code, but please note I know this code is probably very wrong, and it obviously doesn't work. But hopefully, it will help illustrate what I'm trying to do. Thank you in advance.

import tkinter

class MyGui:
    
 def __init__(self):
    
  self.window = tkinter.Tk()


  self.e1 = tkinter.Entry(self.window)
  self.e1.pack(fill='x')

  self.e2 = tkinter.Entry(self.window)
  self.e2.pack(fill='x')
  
  
  self.label = tkinter.Label(self.window)
  self.label.pack(fill='x')
  
  self.button1 = tkinter.Button(self.window, text="add", font=('Arial', 16), command=self.add)
  self.button1.pack()


  self.window.mainloop()


 def add(self):
  entry1 = self.e1.get
  entry1 = int(entry1)

  entry2 = self.e2.get
  entry2 = int(entry2)

  self.label = entry1 + entry2 
  
  

  

MyGui()

Solution

  • You need to add parenthesis to get to call the function otherwise it will just get the function reference.

    And use self.label.config(text=entry1 + entry2) to update the text.

    With all the above changes here is the result:

    import tkinter
    
    class MyGui:
        
     def __init__(self):
        
      self.window = tkinter.Tk()
    
    
      self.e1 = tkinter.Entry(self.window)
      self.e1.pack(fill='x')
    
      self.e2 = tkinter.Entry(self.window)
      self.e2.pack(fill='x')
    
      self.label = tkinter.Label(self.window, text="Enter something and click Add to see something here!")
      self.label.pack(fill='x')
      
      self.button1 = tkinter.Button(self.window, text="add", font=('Arial', 16), command=self.add)
      self.button1.pack()
    
    
      self.window.mainloop()
    
    
     def add(self):
      entry1 = self.e1.get()
      entry1 = int(entry1)
    
      entry2 = self.e2.get()
      entry2 = int(entry2)
    
      self.label.config(text=entry1 + entry2)
      
    
    MyGui()