【问题标题】:not getting value of tkinter of Entry widget [duplicate]没有获得 Entry 小部件的 tkinter 的值 [重复]
【发布时间】:2019-02-26 14:14:17
【问题描述】:

这是我的入口小部件和按钮点击代码

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

&我的函数 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)

输出是这样的

amount entered : 

在文本小部件中放入 200 时没有获得 textvariable 值

【问题讨论】:

    标签: python oop tkinter


    【解决方案1】:

    这段代码没有运行,所以我猜到它应该是什么样子了。

    您需要在使用之前创建StringVar()amount_entry。您可以在函数 perform_deposit() 之外执行此操作,而不必将其声明为 global

    当您将按钮与命令相关联时,不应包含括号,因为在声明按钮命令时会运行该函数。

    检查以下示例:

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

    【讨论】:

      猜你喜欢
      • 2018-05-02
      • 2012-04-06
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多