Skip to content Skip to sidebar Skip to footer

Tkinter: Link An Entry Widget To A Button To A Function

I am new to Tkinter and not to sure how to proceed. I am trying to link a function that I define to a entry widget that is activated by a button. but I can't figure out how to get

Solution 1:

Button calls function assigned to command= (it has to be "function name" without () and arguments - or lambda function)

TestMath assigns calculation to global variable result and other functions can have access to that value.

import Tkinter as tk


def TestMath():
    global result # to return calculation

    result = int(entry.get())
    result += 4

    print result

result = 0

root = tk.Tk()

entry = tk.Entry(root)
entry.pack()

button = tk.Button(root, text="Calculate", command=TestMath)
button.pack()

root.mainloop()

Function called by button don't have to return value because there is no object which could receive that value.

Post a Comment for "Tkinter: Link An Entry Widget To A Button To A Function"