Python Tkinter:我可以通过输入多个条目进行计算并将结果显示为文本吗?
Python Tkinter: Can i do a calculation with the input of several Entries and display the result as text?
好的 - 我有不到 24 小时的编程经验 - 这是我的第二天 - 所以请不要屠杀我。昨天我几乎花了一整天的时间来弄清楚这个问题,但是所有的概念对我来说还是有点模糊-我希望有人愿意提供帮助。
我想根据几个 Entry 小部件的输入进行计算,然后将结果显示为标签(或仅文本)中的输出。
Here's a picture
这是一段代码
from tkinter import *
from tkinter.messagebox import *
root = Tk()
root.title("StoGram 1.0")
# Entry
symentry = Entry(root, width=10) #Symbol
enentry = Entry(root, width=10) #Entry
slentry = Entry(root, width=10) #Stop Loss
tpentry = Entry(root, width=10) #Take Profit
kbentry = Entry(root, width=10) #Bank Balance
rentry = Entry(root, width=10) #Max Risk
symentry.grid(row=1, column=3) #Symbol
enentry.grid(row=2, column=1) #Entry
slentry.grid(row=3, column=1) #Stop Loss
tpentry.grid(row=4, column=1) #Take Profit
kbentry.grid(row=5, column=1) #Bank Balance
kbentry.insert(0, "100000")
rentry.grid(row=6, column=1) #Max Risk
rentry.insert(0, "1")
# Calculate button to grid
calculate_Button = Button(root, text='Calculate', command=answer)
# Calculate button to grid
calculate_button.grid(row="4", column="3")
我想弄清楚如何处理 # Calculate button to grid calculate_Button = Button(root, text='Calculate', command=answer)
并在尝试中尝试过
def answer():
以及 int
和 str
.get()
以及其他一些东西。
但实际上,我只是一只拿着扳手锤子试图让东西工作的猴子,我不知道自己在做什么。
非常感谢任何建议。
我想这可能对你有帮助。
import tkinter as tk
root = tk.Tk()
root.title("StoGram 1.0")
bank_balance = tk.StringVar()
max_risk =tk.StringVar()
entry = tk.StringVar()
stop_loss= tk.StringVar()
def answer():
output = (int(bank_balance.get())*int(max_risk.get()))/(int(entry.get())-int(stop_loss.get()))
print(output)
Output_label.config(text = str(output))
# Entry
symentry = tk.Entry(root, width=10) #Symbol
enentry = tk.Entry(root, width=10, textvariable = entry) #Entry
slentry = tk.Entry(root, width=10, textvariable = stop_loss) #Stop Loss
tpentry = tk.Entry(root, width=10) #Take Profit
kbentry = tk.Entry(root, width=10,textvariable = bank_balance) #Bank Balance
rentry = tk.Entry(root, width=10, textvariable = max_risk) #Max Risk
symentry_label = tk.Label(root, text = 'Symbol', font=('calibre',10))
enentry_label = tk.Label(root, text = 'Entry:', font=('calibre',10))
slentry_label = tk.Label(root, text = 'Stop Loss:', font=('calibre',10))
tpentry_label = tk.Label(root, text = 'Take Profit:', font=('calibre',10))
kbentry_label = tk.Label(root, text = 'Bank Balance:', font=('calibre',10))
rentry_label = tk.Label(root, text = 'Max Risk%', font=('calibre',10))
Buy_shares_label = tk.Label(root, text = 'Buy Shares:', font=('calibre',10))
Output_label = tk.Label(root, text = '', font=('calibre',10))
Rps_Label = tk.Label(root, text = 'Rps:', font=('calibre',10))
R_Label = tk.Label(root, text = 'R:', font=('calibre',10))
symentry_label.grid(row=1, column=3)
enentry_label.grid(row=3, column=1)
slentry_label.grid(row=4, column=1)
tpentry_label.grid(row=5, column=1)
kbentry_label.grid(row=6, column=1)
rentry_label.grid(row=7, column=1)
Rps_Label.grid(row=4,column=4)
Buy_shares_label.grid(row=5,column=4)
Output_label.grid(row=5,column=6)
R_Label.grid(row=6,column=4)
symentry.grid(row=2, column=3) #Symbol
enentry.grid(row=3, column=2) #Entry
slentry.grid(row=4, column=2) #Stop Loss
tpentry.grid(row=5, column=2) #Take Profit
kbentry.grid(row=6, column=2) #Bank Balance
kbentry.insert(0, "100000")
rentry.grid(row=7, column=2) #Max Risk
rentry.insert(0, "1")
# Calculate button to grid
calculate_Button = tk.Button(root, text='Calculate', command=answer)
# Calculate button to grid
calculate_Button.grid(row="5", column="3")
root.mainloop()
要进行计算,您需要首先创建一个函数,如您所说,名为answer。 def answer()
然后在里面你可以做所有的计算。
然后最重要的工作是从文本输入中提取值,所以我们需要创建变量将它们初始化为 tk.StringVar()
这将使变量成为一种全局变量,可以在整个过程中使用程序。然后我们需要将变量与我们需要使用 textvariable = Variable Name
作为其参数之一的 tk.Entry()
连接起来。
我们还需要使用 int()
将变量转换为 整数 ,因为我们从 文本输入 获得的值是以 strings.
的形式
好的 - 我有不到 24 小时的编程经验 - 这是我的第二天 - 所以请不要屠杀我。昨天我几乎花了一整天的时间来弄清楚这个问题,但是所有的概念对我来说还是有点模糊-我希望有人愿意提供帮助。
我想根据几个 Entry 小部件的输入进行计算,然后将结果显示为标签(或仅文本)中的输出。
Here's a picture
这是一段代码
from tkinter import *
from tkinter.messagebox import *
root = Tk()
root.title("StoGram 1.0")
# Entry
symentry = Entry(root, width=10) #Symbol
enentry = Entry(root, width=10) #Entry
slentry = Entry(root, width=10) #Stop Loss
tpentry = Entry(root, width=10) #Take Profit
kbentry = Entry(root, width=10) #Bank Balance
rentry = Entry(root, width=10) #Max Risk
symentry.grid(row=1, column=3) #Symbol
enentry.grid(row=2, column=1) #Entry
slentry.grid(row=3, column=1) #Stop Loss
tpentry.grid(row=4, column=1) #Take Profit
kbentry.grid(row=5, column=1) #Bank Balance
kbentry.insert(0, "100000")
rentry.grid(row=6, column=1) #Max Risk
rentry.insert(0, "1")
# Calculate button to grid
calculate_Button = Button(root, text='Calculate', command=answer)
# Calculate button to grid
calculate_button.grid(row="4", column="3")
我想弄清楚如何处理 # Calculate button to grid calculate_Button = Button(root, text='Calculate', command=answer)
并在尝试中尝试过
def answer():
以及 int
和 str
.get()
以及其他一些东西。
但实际上,我只是一只拿着扳手锤子试图让东西工作的猴子,我不知道自己在做什么。
非常感谢任何建议。
我想这可能对你有帮助。
import tkinter as tk
root = tk.Tk()
root.title("StoGram 1.0")
bank_balance = tk.StringVar()
max_risk =tk.StringVar()
entry = tk.StringVar()
stop_loss= tk.StringVar()
def answer():
output = (int(bank_balance.get())*int(max_risk.get()))/(int(entry.get())-int(stop_loss.get()))
print(output)
Output_label.config(text = str(output))
# Entry
symentry = tk.Entry(root, width=10) #Symbol
enentry = tk.Entry(root, width=10, textvariable = entry) #Entry
slentry = tk.Entry(root, width=10, textvariable = stop_loss) #Stop Loss
tpentry = tk.Entry(root, width=10) #Take Profit
kbentry = tk.Entry(root, width=10,textvariable = bank_balance) #Bank Balance
rentry = tk.Entry(root, width=10, textvariable = max_risk) #Max Risk
symentry_label = tk.Label(root, text = 'Symbol', font=('calibre',10))
enentry_label = tk.Label(root, text = 'Entry:', font=('calibre',10))
slentry_label = tk.Label(root, text = 'Stop Loss:', font=('calibre',10))
tpentry_label = tk.Label(root, text = 'Take Profit:', font=('calibre',10))
kbentry_label = tk.Label(root, text = 'Bank Balance:', font=('calibre',10))
rentry_label = tk.Label(root, text = 'Max Risk%', font=('calibre',10))
Buy_shares_label = tk.Label(root, text = 'Buy Shares:', font=('calibre',10))
Output_label = tk.Label(root, text = '', font=('calibre',10))
Rps_Label = tk.Label(root, text = 'Rps:', font=('calibre',10))
R_Label = tk.Label(root, text = 'R:', font=('calibre',10))
symentry_label.grid(row=1, column=3)
enentry_label.grid(row=3, column=1)
slentry_label.grid(row=4, column=1)
tpentry_label.grid(row=5, column=1)
kbentry_label.grid(row=6, column=1)
rentry_label.grid(row=7, column=1)
Rps_Label.grid(row=4,column=4)
Buy_shares_label.grid(row=5,column=4)
Output_label.grid(row=5,column=6)
R_Label.grid(row=6,column=4)
symentry.grid(row=2, column=3) #Symbol
enentry.grid(row=3, column=2) #Entry
slentry.grid(row=4, column=2) #Stop Loss
tpentry.grid(row=5, column=2) #Take Profit
kbentry.grid(row=6, column=2) #Bank Balance
kbentry.insert(0, "100000")
rentry.grid(row=7, column=2) #Max Risk
rentry.insert(0, "1")
# Calculate button to grid
calculate_Button = tk.Button(root, text='Calculate', command=answer)
# Calculate button to grid
calculate_Button.grid(row="5", column="3")
root.mainloop()
要进行计算,您需要首先创建一个函数,如您所说,名为answer。 def answer()
然后在里面你可以做所有的计算。
然后最重要的工作是从文本输入中提取值,所以我们需要创建变量将它们初始化为 tk.StringVar()
这将使变量成为一种全局变量,可以在整个过程中使用程序。然后我们需要将变量与我们需要使用 textvariable = Variable Name
作为其参数之一的 tk.Entry()
连接起来。
我们还需要使用 int()
将变量转换为 整数 ,因为我们从 文本输入 获得的值是以 strings.