-3

this is my code for entry widget & button click

# 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)

& my function perform_deposit

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)

Output is like

amount entered : 

not getting textvariable value on putting 200 in text widget

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685

1 Answers1

0

This code does not run, so I have guessed how it's supposed to look.

You need to create the StringVar() amount_entry before using it. You can do this outside the function perform_deposit() and you don't have to declare it as a global.

When you associate a button with a command you should not include the parentheses because that runs the function when you declare the button command.

Examine the example below:

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()
figbeam
  • 7,001
  • 2
  • 12
  • 18