简体   繁体   中英

How to reset StringVar in tkinter

I am currently working on a calculator and I was coding the C Button (C button is the button which erases the current operation) however, My idea was to make the C Button return all Variables used during the operation to their starting state so we can start a new operation after pressing C. The problem is there is something called StringVar() in Tkinter that I used after reading an answer on StackOverflow. It is used to update the label every time I hit a new Button. it works but when trying to return it to its normal state when pressing the C button it doesn't work. They are still the same. so how can I reset the StringVar?

solution: Turns out I didn't assign the global variables used in the function, but any way to reset the stringVar you can just do this: (variable name).set("")

It should be variableName.set("")

s = StringVar()

# here getting textvariable

value = s.get()
print(value)  # input value

s.set("")  # reset it

Edit: After below comment, I am adding this structure to show how to keep desired item clean

# getting input
sFaculty = StringVar()
combo = ttk.Combobox(tab1, ..., textvariable=sFaculty, ...)

# assing it to function
Button(tab1, text="Insert", command=lambda: guiActions.insertStudent(..., sFaculty, ...))

# process and clean in a function
def insertStudent(..., faculty, ...):

    # process here

    # cleaning after insert
    faculty.set("")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM