Python 未获取条目小部件的tkinter值
这是我的输入小部件和按钮单击的代码Python 未获取条目小部件的tkinter值,python,oop,tkinter,Python,Oop,Tkinter,这是我的输入小部件和按钮单击的代码 # Amount entry here textBoxAmount = Entry(win, textvariable=amount_entry) textBoxAmount.grid(row=2, column=1) # Deposit button here buttonDeposit = tk.Button(text="Deposit", command=perform_deposit()) buttonDepo
# Amount entry here
textBoxAmount = Entry(win, textvariable=amount_entry)
textBoxAmount.grid(row=2, column=1)
# Deposit button here
buttonDeposit = tk.Button(text="Deposit", command=perform_deposit())
buttonDeposit.grid(row=2, column=2)
&我的职能是执行存款
def perform_deposit():
'''Function to add a deposit for the amount in the amount entry to the
account's transaction list.'''
global account
global amount_entry
global balance_label
global balance_var
# Try to increase the account balance and append the deposit to the account file
#input = amount_text.get("1.0",END)
amount_entered = amount_entry.get()
print("amount entered : {}".format(amount_entry.get()))
print(amount_entered)
#balance_var= account.deposit(amount_entry.get())
print(balance_var)
输出就像
amount entered :
在文本小部件中放入200时未获得textvariable值这段代码没有运行,所以我猜它应该是什么样子 在使用之前,您需要创建
StringVar()
amount\u条目。您可以在函数perform_deposit()
之外执行此操作,而不必将其声明为全局
将按钮与命令关联时,不应包含括号,因为在声明按钮命令时,括号将运行函数
检查以下示例:
from tkinter import *
win = Tk()
win.geometry('300x200')
amount_entry = StringVar()
def perform_deposit():
global balance_var
amount_entered = amount_entry.get()
print("amount entered : {}".format(amount_entry.get()))
# Amount entry here
textBoxAmount = Entry(win, textvariable=amount_entry)
textBoxAmount.grid(row=2, column=1)
# Deposit button here
buttonDeposit = Button(text="Deposit", command=perform_deposit)
buttonDeposit.grid(row=2, column=2)
win.mainloop()