import tkinter as tk from tkinter import ttk from forex_python.converter import CurrencyRates # 创建货币转换器对象 c = CurrencyRates() # 创建主窗口 root = tk.Tk() root.title('货币转换器') # 创建控件 amount_label = ttk.Label(root, text='金额:') amount_entry = ttk.Entry(root) from_currency_label = ttk.Label(root, text='从货币类型:') from_currency_combo = ttk.Combobox(root, values=['USD', 'EUR', 'JPY', 'GBP']) to_currency_label = ttk.Label(root, text='到货币类型:') to_currency_combo = ttk.Combobox(root, values=['USD', 'EUR', 'JPY', 'GBP']) convert_button = ttk.Button(root, text='转换') result_label = ttk.Label(root, text='转换结果:') result_value = ttk.Label(root, text='') # 控件布局 amount_label.grid(row=0, column=0, padx=5, pady=5, sticky='w') amount_entry.grid(row=0, column=1, padx=5, pady=5) from_currency_label.grid(row=1, column=0, padx=5, pady=5, sticky='w') from_currency_combo.grid(row=1, column=1, padx=5, pady=5) to_currency_label.grid(row=2, column=0, padx=5, pady=5, sticky='w') to_currency_combo.grid(row=2, column=1, padx=5, pady=5) convert_button.grid(row=3, column=0, columnspan=2, padx=5, pady=5) result_label.grid(row=4, column=0, padx=5, pady=5, sticky='w') result_value.grid(row=4, column=1, padx=5, pady=5) # 定义转换函数 def convert(): amount = float(amount_entry.get()) from_currency = from_currency_combo.get() to_currency = to_currency_combo.get() result = c.convert(from_currency, to_currency, amount) result_value.config(text=result) # 绑定事件 convert_button.config(command=convert) # 运行主循环 tk.mainloop() 优化此程序使窗口能运行
时间: 2024-03-01 22:54:00 浏览: 141
code09_tkinter_login.pyw_python_tkinter_notepad.pyw_
以下是优化后的代码:
```python
import tkinter as tk
from tkinter import ttk
from forex_python.converter import CurrencyRates
class CurrencyConverter:
def __init__(self, root):
self.root = root
self.root.title('货币转换器')
self.c = CurrencyRates()
self.create_widgets()
def create_widgets(self):
# 创建控件
amount_label = ttk.Label(self.root, text='金额:')
self.amount_entry = ttk.Entry(self.root)
from_currency_label = ttk.Label(self.root, text='从货币类型:')
self.from_currency_combo = ttk.Combobox(self.root, values=['USD', 'EUR', 'JPY', 'GBP'])
to_currency_label = ttk.Label(self.root, text='到货币类型:')
self.to_currency_combo = ttk.Combobox(self.root, values=['USD', 'EUR', 'JPY', 'GBP'])
convert_button = ttk.Button(self.root, text='转换', command=self.convert)
result_label = ttk.Label(self.root, text='转换结果:')
self.result_value = ttk.Label(self.root, text='')
# 控件布局
amount_label.grid(row=0, column=0, padx=5, pady=5, sticky='w')
self.amount_entry.grid(row=0, column=1, padx=5, pady=5)
from_currency_label.grid(row=1, column=0, padx=5, pady=5, sticky='w')
self.from_currency_combo.grid(row=1, column=1, padx=5, pady=5)
to_currency_label.grid(row=2, column=0, padx=5, pady=5, sticky='w')
self.to_currency_combo.grid(row=2, column=1, padx=5, pady=5)
convert_button.grid(row=3, column=0, columnspan=2, padx=5, pady=5)
result_label.grid(row=4, column=0, padx=5, pady=5, sticky='w')
self.result_value.grid(row=4, column=1, padx=5, pady=5)
def convert(self):
amount = float(self.amount_entry.get())
from_currency = self.from_currency_combo.get()
to_currency = self.to_currency_combo.get()
result = self.c.convert(from_currency, to_currency, amount)
self.result_value.config(text=result)
if __name__ == '__main__':
# 创建主窗口并运行
root = tk.Tk()
currency_converter = CurrencyConverter(root)
root.mainloop()
```
这里将原来的代码封装在一个类 `CurrencyConverter` 中,使得代码更加模块化和易读。将 `convert()` 方法绑定到转换按钮上,实现转换功能。同时将控件中需要动态修改的部分(如输入框、下拉框、结果标签)设为实例属性,方便在 `convert()` 方法中调用和修改。最后在 `__main__` 中创建主窗口并运行。
阅读全文