Tkinter多Entry组件输入内容同步异常问题求助
Hey there! I see that weird sync issue with your Entry widgets—let’s get that sorted out right away.
The Root Cause
You added a text="test" parameter to every tk.Entry() call, but Tkinter’s Entry component doesn’t support a text argument. When you pass this invalid parameter, Tkinter incorrectly binds all your Entry widgets to a shared default text variable. That’s why typing in one box updates all the others—they’re all linked to the same underlying value.
The Fix
Simply remove the text="test" parameter from each Entry definition. Here’s your corrected code:
import tkinter as tk window = tk.Tk() window.title('Mortgage Calculator') # 房产总价是多少? property_price = tk.Label( window, text="How much is the property? ", font=("Arial", 11)) property_price.pack(pady=10) property_price_entry = tk.Entry(window, font=("Arial", 11)) property_price_entry.pack(pady=10) # 您打算支付多少首付? property_deposit = tk.Label( window, text="How much do you want to pay as a downpayment? ", font=("Arial", 11)) property_deposit.pack(pady=10) property_deposit_entry = tk.Entry(window, font=("Arial", 11)) property_deposit_entry.pack(pady=10) # 您计划多少年还清抵押贷款? years_of_mortgage = tk.Label( window, text="How many years are you paying off your mortgage? ", font=("Arial", 11)) years_of_mortgage.pack(pady=10) years_of_mortgage_entry = tk.Entry(window, font=("Arial", 11)) years_of_mortgage_entry.pack(pady=10) # 贷款利率是多少? interest = tk.Label( window, text="How much in interest are you paying? ", font=("Arial", 11)) interest.pack(pady=10) interest_entry = tk.Entry(window, font=("Arial", 11)) interest_entry.pack(pady=10) # 抵押贷款计算按钮 monthly_mortgage_payment = tk.Label( window, text="Calculate monthly mortgage payments ", font=("Arial", 11)) monthly_mortgage_payment.pack(pady=10) monthly_mortgage_payment_button = tk.Button( window, text="Calculate monthly mortgage payments ", font=("Arial", 11)) monthly_mortgage_payment_button.pack(pady=10) window.mainloop()
Bonus: Setting Default Text Properly
If you wanted to add default text to your Entry widgets (like the "test" you tried), use one of these valid methods instead:
- Use the
insert()method after creating the Entry:property_price_entry = tk.Entry(window, font=("Arial", 11)) property_price_entry.insert(0, "test") # Inserts "test" at the start of the Entry - Bind each Entry to its own
StringVar(perfect if you need to track or update values programmatically):price_var = tk.StringVar(value="test") property_price_entry = tk.Entry(window, textvariable=price_var, font=("Arial", 11))
Now each Entry widget will work independently, and you can input different values into each one without them syncing up!
内容的提问来源于stack exchange,提问作者Gabriella Dines

