为什么我不能 运行 在第二个 运行 中设置代码?
Why i can't running the code in the second run?
我的代码在第一个 运行 中没有错误,但在第二个 运行 之后(尚未关闭 window)是错误的。这是我的代码:
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()
如果我第一次输入 5000 * 4 这样的整数,这就是输出:
20000
如果我在第二个输入另一个整数,如 2000 * 2,我得到错误:
AttributeError: 'int' object has no attribute 'get'
希望大家明白我的问题,谢谢
由于您使用全局变量来存储文本字段,因此您不能将它们重新用作输入值的变量,
要修复它,您只需使用一个新变量来存储从文本字段中提取的值
def mat():
global harga, jumlah, total
harga_value = int(harga.get())
jumlah_value = int(jumlah.get())
total = harga_value * jumlah_value
return mat
我的代码在第一个 运行 中没有错误,但在第二个 运行 之后(尚未关闭 window)是错误的。这是我的代码:
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()
如果我第一次输入 5000 * 4 这样的整数,这就是输出:
20000
如果我在第二个输入另一个整数,如 2000 * 2,我得到错误:
AttributeError: 'int' object has no attribute 'get'
希望大家明白我的问题,谢谢
由于您使用全局变量来存储文本字段,因此您不能将它们重新用作输入值的变量,
要修复它,您只需使用一个新变量来存储从文本字段中提取的值
def mat():
global harga, jumlah, total
harga_value = int(harga.get())
jumlah_value = int(jumlah.get())
total = harga_value * jumlah_value
return mat