使用 Python Tkinter 创建带计算功能的窗口
要生成一个窗口,并在窗口中包含所需的输入框、按钮和显示屏幕,可以使用 Python 的 GUI 库 tkinter。以下是一个示例代码:
import tkinter as tk
def calculate():
weight = float(weight_entry.get())
cost = float(cost_entry.get())
discount = float(discount_entry.get())
profit_margin = float(profit_margin_entry.get())
exchange_rate = float(exchange_rate_entry.get())
commission = float(commission_entry.get())
price = (cost + commission) / (1 - profit_margin)
total_price = price * (1 - discount)
total_price_in_rmb = total_price * exchange_rate
output_text.delete(1.0, tk.END)
output_text.insert(tk.END, f'Price: {price}
')
output_text.insert(tk.END, f'Total Price: {total_price}
')
output_text.insert(tk.END, f'Total Price in RMB: {total_price_in_rmb}
')
window = tk.Tk()
window.title('Calculator')
window.geometry('400x500')
weight_label = tk.Label(window, text='Weight')
weight_label.pack()
weight_entry = tk.Entry(window)
weight_entry.pack()
cost_label = tk.Label(window, text='Cost')
cost_label.pack()
cost_entry = tk.Entry(window)
cost_entry.pack()
discount_label = tk.Label(window, text='Discount')
discount_label.pack()
discount_entry = tk.Entry(window)
discount_entry.pack()
profit_margin_label = tk.Label(window, text='Profit Margin')
profit_margin_label.pack()
profit_margin_entry = tk.Entry(window)
profit_margin_entry.pack()
exchange_rate_label = tk.Label(window, text='Exchange Rate')
exchange_rate_label.pack()
exchange_rate_entry = tk.Entry(window)
exchange_rate_entry.pack()
commission_label = tk.Label(window, text='Commission')
commission_label.pack()
commission_entry = tk.Entry(window)
commission_entry.pack()
calculate_button = tk.Button(window, text='Calculate', command=calculate)
calculate_button.pack()
output_text = tk.Text(window, width=30, height=40)
output_text.pack()
window.mainloop()
运行这段代码,就会生成一个包含所需功能的窗口。用户可以在输入框中输入相关参数,然后点击计算按钮,计算结果将会显示在屏幕上。
原文地址: https://www.cveoy.top/t/topic/oX8r 著作权归作者所有。请勿转载和采集!