我正在嘗試使用 Python 中的 GUI tkinker 制作一個基本的利息收入計算器。但是,在輸入所有值后,末尾的標簽不會更新。
請注意,計算只是暫時列印變數
import tkinter as tk
frame = tk.Tk()
frame.title("Interest Income Calculator")
frame.geometry('800x450')
label = tk.Label(text="Enter Balance (number, no symbols)")
entry = tk.Entry(fg="Black", bg="White", width=50)
label.pack()
entry.pack()
def update1():
global balance
balance = entry.get()
buttonupdate1 = tk.Button(
text = "Save")
buttonupdate1.pack()
buttonupdate1.config(command = update1())
label2 = tk.Label(text="Enter Standard Interest (percentage but no symbol)")
entry2 = tk.Entry(fg="Black", bg="White", width=50)
label2.pack()
entry2.pack()
def update2():
global sinterest
sinterest = entry2.get()
buttonupdate2 = tk.Button(
text = "Save")
buttonupdate2.pack()
buttonupdate2.config(command = update2())
label3 = tk.Label(text="Enter Bonus Interest (percentage but no symbol)")
entry3 = tk.Entry(fg="Black", bg="White", width=50)
label3.pack()
entry3.pack()
def update3():
global binterest
binterest = entry3.get()
buttonupdate3 = tk.Button(
text = "Save")
buttonupdate3.pack()
buttonupdate3.config(command = update3())
button = tk.Button(
text="Calculate",
width=25,
height=1,
background="white",
foreground="black",
)
button.pack()
calculations = (balance, sinterest, binterest)
label4 = tk.Label(
text = (calculations),
foreground = "black",
background="white",
width=25,
height=10
)
def cont():
label4.pack()
button.config(command=cont())
frame.mainloop()
我已經嘗試了很多東西,比如在函式中移動等等,但沒有運氣。請幫忙
uj5u.com熱心網友回復:
要理解為什么它在我們使用時不起作用,command = func()我們必須理解將函式作為引數傳遞是如何作業的
當我們將帶有括號的函式作為引數傳遞時,我們首先呼叫該函式,然后將回傳值作為我們正在使用的方法或函式的引數。例如
def sum():
return 1 1
example1 = sum
example2 = sum()
# -> example1 is a function object
# -> example2 is 2
這就是為什么在你的代碼中你必須給你的按鈕一個函式物件而不是函式的回傳值,這樣當你按下按鈕時它可以運行函式物件。
我用這些修復程式運行了你的代碼,發現它沒有運行。這是因為在你的代碼中你定義了你的變數sinterest,在你balance的binterest函式內部它會拋出一個錯誤,因為現在你在代碼運行時不運行這些函式。
您還試圖更改元組內的值,這是不可能的,因為元組是不可變的
calculations = (balance, sinterest, binterest)
您應該將其更改為串列,因為您可以更改串列中的值
calculations = [balance, sinterest, binterest]
uj5u.com熱心網友回復:
(command = function) 因為你傳遞的是函式而不是呼叫函式()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/533596.html
