一个使用tkinter库编写的更复杂的面向对象代码示例
import tkinter as tk
class CalculatorApp:
def __init__(self):
self.root = tk.Tk()
self.root.title("计算器")
self.result = tk.StringVar()
self.create_widgets()
def create_widgets(self):
# 显示结果的标签
result_label = tk.Label(
self.root, textvariable=self.result, relief=tk.RIDGE, width=20, font=("Helvetica", 20)
)
result_label.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
# 数字按钮
buttons = [
('7', 1, 0), ('8', 1, 1), ('9', 1, 2), ('/', 1, 3),
('4', 2, 0), ('5', 2, 1), ('6', 2, 2), ('*', 2, 3),
('1', 3, 0), ('2', 3, 1), ('3', 3, 2), ('-', 3, 3),
('0', 4, 0), ('+', 4, 1), ('^', 4, 2), ('=', 4, 3)
]
for text, row, col in buttons:
btn = tk.Button(
self.root, text=text, command=lambda t=text: self.handle_button_click(t),
width=5, height=2, font=("Helvetica", 12)
)
btn.grid(row=row, column=col, padx=5, pady=5)
# 清除按钮
clear_btn = tk.Button(
self.root, text="清除", command=self.clear_result,
width=10, height=2, font=("Helvetica", 12)
)
clear_btn.grid(row=5, column=0, columnspan=2, padx=5, pady=5)
def handle_button_click(self, button_text):
current_result = self.result.get()
if button_text == "=":
try:
result = eval(current_result)
self.result.set(result)
except Exception:
self.result.set("错误")
else:
self.result.set(current_result + button_text)
def clear_result(self):
self.result.set("")
def run(self):
self.root.mainloop()
if __name__ == "__main__":
app = CalculatorApp()
app.run()
在这个例子中,我们创建了一个名为CalculatorApp的类来实现一个简单的计算器应用程序。应用程序窗口包含一个显示结果的标签、数字按钮、清除按钮和计算按钮。
在create_widgets方法中,我们首先创建了一个Label组件来显示计算结果。然后使用一个列表中的循环创建数字按钮,并为每个按钮添加了相应的点击事件处理方法。清除按钮和计算按钮也分别添加了相应的点击事件处理方法。
点击数字按钮时,handle_button_click方法将获取当前结果并将点击的按钮文本追加到结果字符串后面。清除按钮将清空结果字符串,而计算按钮将根据结果字符串中的表达式计算结果并显示在结果标签上。
最后,通过实例化CalculatorApp类并调用run方法来启动应用程序的事件循环。
请注意,这只是一个简单示例,仅用于演示如何使用tkinter库编写面向对象的GUI代码。你可以根据自己的需求进行进一步扩展和修改。
result_label标签使用更大的字体,并设置了内边距(padx和pady)来增加标签的间距。- 数字按钮和清除按钮都设置了按钮的宽度、高度和字体。
- 使用 padx 和 pady 来给按钮添加一些间距。
# 设置按钮颜色和字体样式
button_style = ttk.Style()
button_style.configure("TButton", font=("Arial", 12))
# 设置数字按钮的背景颜色和前景颜色
button_style.configure("Numpad.TButton", background="white", foreground="black")
# 设置运算符按钮的背景颜色和前景颜色
button_style.configure("Operator.TButton", background="#FF9500", foreground="white")
# 创建数字和运算符按钮
buttons = [
('7', 1, 0), ('8', 1, 1), ('9', 1, 2), ('/', 1, 3),
('4', 2, 0), ('5', 2, 1), ('6', 2, 2), ('*', 2, 3),
('1', 3, 0), ('2', 3, 1), ('3', 3, 2), ('-', 3, 3),
('0', 4, 0), ('+', 4, 1), ('^', 4, 2), ('=', 4, 3)
]
# 遍历按钮列表创建按钮并设置样式
for button_text, row, col in buttons:
button = ttk.Button(root, text=button_text, style="Numpad.TButton")
# 将按钮放置到网格中的指定位置
button.grid(row=row, column=col, ipadx=10, ipady=10, padx=5, pady=5, sticky="nsew")
# 根据按钮的文本设置样式
if button_text in ['/', '*', '-', '+', '^', '=']:
button.configure(style="Operator.TButton")
# 设置结果标签的字体样式和对齐方式
result_label = ttk.Label(root, textvariable=result, font=("Arial", 14), anchor="e")
result_label.grid(row=0, column=0, columnspan=4, ipadx=10, ipady=10, padx=5, pady=5, sticky="ew")
# 设置窗口的内边距和外边距
root.grid_rowconfigure(0, weight=1)
root.grid_rowconfigure(1, weight=1)
root.grid_rowconfigure(2, weight=1)
root.grid_rowconfigure(3, weight=1)
root.grid_rowconfigure(4, weight=1)
root.grid_columnconfigure(0, weight=1)
root.grid_columnconfigure(1, weight=1)
root.grid_columnconfigure(2, weight=1)
root.grid_columnconfigure(3, weight=1)
root.config(padding="20 20 20 20")
'''
这段代码主要用于定制化计算器界面的样式。它使用了 ttk.Style() 来创建一个样式对象,并通过 configure 方法为按钮设置不同的样式。具体解释如下:
button_style.configure("TButton", font=("Arial", 12)):设置所有按钮的字体样式为 Arial 字体,大小为 12 号字体。
button_style.configure("Numpad.TButton", background="white", foreground="black"):对数字按钮应用 Numpad.TButton 样式,设置背景颜色为白色,前景颜色(文本颜色)为黑色。
button_style.configure("Operator.TButton", background="#FF9500", foreground="white"):对运算符按钮应用 Operator.TButton 样式,设置背景颜色为橙色(#FF9500),前景颜色为白色。
然后,通过遍历按钮列表,创建并放置按钮到网格中。对于数字按钮,设置样式为 Numpad.TButton,对于运算符按钮,设置样式为 Operator.TButton。
最后,设置结果标签的字体样式、对齐方式,并对窗口进行内边距和外边距的设置。
'''
#python#Python 文章被收录于专栏
Python由荷兰数学和计算机科学研究学会的吉多·范罗苏姆于1990年代初设计,作为一门叫做ABC语言的替代品。Python提供了高效的高级数据结构,还能简单有效地面向对象编程。Python语法和动态类型,以及解释型语言的本质,使它成为多数平台上写脚本和快速开发应用的编程语言,随着版本的不断更新和语言新功能的添加,逐渐被用于独立的、大型项目的开发
美的集团公司福利 727人发布