2

my code is not error in the first run, but after the second run (not closed the window yet) is error. This is my code:

    import tkinter as tk
    from tkinter import *

    window = tk.Tk()
    window.state('zoomed')

    Harga = []

    inputH = IntVar(window)
    inputJ = IntVar(window)
    harga = Entry(window, width = 80, textvariable = inputH)
    jumlah = Entry(window, width = 80, textvariable = inputJ)

    def mat():
      global harga, jumlah, total, teks, Harga
      harga = int(harga.get())
      jumlah = int(jumlah.get())
      total = harga * jumlah
      print(total)
      return mat

    tombol = FALSE
    def perulangan():
      global tombol
      tombol = TRUE
      if tombol == TRUE:
        mat()
      else:
        pass
     return perulangan


   tombol = Button(window, text = "Submit", command = perulangan)
   keluar = Button(window, text = "Quit", background = 'red')

   harga.pack()
   jumlah.pack()
   tombol.pack()
   keluar.pack()
   window.mainloop()
    

and if i input the integer like 5000 * 4 in the first time, this is the ouput:

  20000

and if i input the other integer like 2000 * 2 in the second, i got error:

  AttributeError: 'int' object has no attribute 'get'

I hope all understand what i ask, thank you.

Arkhan
  • 25
  • 4
  • 4
    It is because you have reassigned `harga` and `jumlah` (original `Entry` widgets) to integer values inside `mat()`. – acw1668 Aug 16 '21 at 01:25
  • 1
    Better not to `return` anything from `mat`, since the code won't use the returned value, and because the current return doesn't make any sense. (`return mat` means that the function returns *itself*, as an object. I guess you meant `return total`, but again there is no use for the computed value since it already got `print`ed and stored in a global variable. But maybe you should use it instead to set the text of another Tkinter widget? – Karl Knechtel Aug 16 '21 at 01:55

1 Answers1

1

Since you are using global variables to store your text field, you cannot reuse them as variables for your inputted values,

to fix it, you can simply use a new variable to store the extracted values from your text fields

def mat():
  global harga, jumlah, total
  harga_value = int(harga.get())
  jumlah_value = int(jumlah.get())
  total = harga_value * jumlah_value
  return mat
kennysliding
  • 2,783
  • 1
  • 10
  • 31