Arkhan
Arkhan

Reputation: 25

Why i can't running the code in the second run?

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.

Upvotes: 2

Views: 189

Answers (1)

kennysliding
kennysliding

Reputation: 2987

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

Upvotes: 1

Related Questions