I am using Tkinter in a programming assignment and have the following problem. I want the user to enter the value in a textbox, and I want to add additional fields on the GUI based on the number entered in the textbox when he/she clicks the submit-button.
I tried to place code inside of the function 'displayText()', which is called when the submit-button is pressed; however, the GUI-related code that I placed inside of it was loaded when the window was loaded.
import tkinter
#When user clicks 开发者_运维知识库on button
def displayText():
#DO CHANGE IN GUI
root = tkinter.Tk()
button = tkinter.Button(root, text="Submit", command=displayText())
button.pack()
root.mainloop()
The problem is in this line of code:
button = tkinter.Button(root, text="Submit", command=displayText())
The command
option takes a reference to a command. What you are doing instead is calling a command (displayText()
) and giving the results of that command to the option. You need to remove the ()
so that the actual command to run is associated with the option, like so:
button = tkinter.Button(root, text="Submit", command=displayText)
精彩评论