python tkinter 使用LabelFrame方法编写UI界面

本文展示了一个使用Python的Tkinter库创建GUI应用程序的例子,包括窗口布局、Treeview组件的配置以及下拉菜单的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

部分内容参考:洪卫的博客 https://www.cnblogs.com/shwee/p/9427975.html


import tkinter as tk  # 使用Tkinter前需要先导入
from tkinter import ttk
# 第1步,实例化object,建立窗口window
window = tk.Tk()
# 第2步,给窗口的可视化起名字
window.title('My Window')
# 第3步,设定窗口的大小(长 * 宽)
window.geometry('1280x1024')  # 这里的乘是小x
# 第4步,place 放置方法(精准的放置到指定坐标点的位置上)
# tk.Label(window, text='Pl', font=('Arial', 20), ).place(x=100, y=100, anchor='nw')
# tk.Entry(window, font=('Arial', 20), ).place(x=200, y=100, anchor='nw')
#tk.Treeview(window, font=('Arial', 20), ).place(x=300, y=100, anchor='nw')  # 表格第一列不显示
ttk.Treeview(window)
tree = ttk.Treeview(window, show="headings")  # 表格第一列不显示
# tree.pack()
# tree.grid(row=2, columnspan=2)
tree.place(x=100, y=300, anchor='nw')
tree["columns"] = (
'序号', '企业名称', '生产经营场所地址', '行业类别', '所在地区', '发证机关', '许可证编号', '办结日期', '有效期限', 'COD年排放量', '氨氮年排放量', '二氧化硫年排放量', '氮氧化物年排放量')
# 设置列,不显示
tree.column("序号", width=50)
tree.column("企业名称", width=100)
tree.column("生产经营场所地址", width=120)
tree.column("行业类别", width=50)
tree.column("所在地区", width=50)
tree.column("发证机关", width=100)
tree.column("许可证编号", width=100)
tree.column("办结日期", width=50)
tree.column("有效期限", width=50)
tree.column("COD年排放量", width=100)
tree.column("氨氮年排放量", width=100)
tree.column("二氧化硫年排放量", width=100)
tree.column("氮氧化物年排放量", width=100)

# 显示表头
tree.heading("序号", text="序号")
tree.heading("企业名称", text="企业名称")
tree.heading("生产经营场所地址", text="生产经营场所地址")
tree.heading("行业类别", text="行业类别")
tree.heading("所在地区", text="所在地区")
tree.heading("发证机关", text="发证机关")
tree.heading("许可证编号", text="许可证编号")
tree.heading("办结日期", text="办结日期")
tree.heading("有效期限", text="有效期限")
tree.heading("COD年排放量", text="COD年排放量")
tree.heading("氨氮年排放量", text="氨氮年排放量")
tree.heading("二氧化硫年排放量", text="二氧化硫年排放量")
tree.heading("氮氧化物年排放量", text="氮氧化物年排放量")


group = ttk.LabelFrame(window,text="第一步选择爬取的地区")
group.place(x=100, y=100, anchor='nw')
# w = tk.Label(group,text="自去年5G商用牌照发放后,5G的发展全面加速,今年则迎来了5G爆发之年。5G网络高速率、业的关注焦点。")
# w.pack(fill = 'y',side = 'left',padx = 10,pady = 50)
com = tk.ttk.Combobox(group) # #创建下拉菜单
com.pack(padx = 10,pady = 10)
com["value"] = '11111'                     #(a_list)  # #给下拉菜单设定值
com2 = tk.ttk.Combobox(group)  # #创建下拉菜单
com2.pack(padx = 10,pady =10)

group = ttk.LabelFrame(window,text="第二步选择是否使用代理IP")
group.place(x=300, y=100, anchor='nw')
# w = tk.Label(group,text="自去年5G商用牌照发放后,5G的发展全面加速,今年则迎来了5G爆发之年。5G网络高速率、业的关注焦点。")
# w.pack(fill = 'y',side = 'left',padx = 10,pady = 50)
com = tk.ttk.Combobox(group) # #创建下拉菜单
com.pack(padx = 10,pady = 10)
com["value"] = '11111'                     #(a_list)  # #给下拉菜单设定值
com2 = tk.ttk.Combobox(group)  # #创建下拉菜单
com2.pack(padx = 10,pady =10)

group = ttk.LabelFrame(window,text="第三步开始爬取")
group.place(x=500, y=100, anchor='nw')
# w = tk.Label(group,text="自去年5G商用牌照发放后,5G的发展全面加速,今年则迎来了5G爆发之年。5G网络高速率、业的关注焦点。")
# w.pack(fill = 'y',side = 'left',padx = 10,pady = 50)
bottom1 = tk.ttk.Button(group, text='开始爬取', command=window.quit) # #创建下拉菜单
bottom1.pack(padx = 10,pady = 10)
bottom2 = tk.ttk.Button(group, text='退出', command=window.quit) # #创建下拉菜单
bottom2.pack(padx = 10,pady = 10)

group = ttk.LabelFrame(window,text="第四步保存")
group.place(x=700, y=100, anchor='nw')
# w = tk.Label(group,text="自去年5G商用牌照发放后,5G的发展全面加速,今年则迎来了5G爆发之年。5G网络高速率、业的关注焦点。")
# w.pack(fill = 'y',side = 'left',padx = 10,pady = 50)
bottom3 = tk.ttk.Button(group, text='保存', command=window.quit) # #创建下拉菜单
bottom3.pack(padx = 10,pady = 10)




# 第5步,主窗口循环显示
window.mainloop()
<think>好的,用户想了解如何用Python编写界面交互程序来实现LoRa数据通信,具体需要教程、示例代码,使用Tkinter或PyQt。首先,我需要确定用户的需求层次。他们可能已经具备一定的Python基础,但对GUI开发和LoRa通信的结合不太熟悉。接下来,我应该考虑如何将这两个部分结合起来,给出一个结构清晰的解决方案。 首先,我应该介绍LoRa通信的基本概念,可能需要使用到串口通信,因为大多数LoRa模块通过串口与主机通信。然后,选择GUI框架,Tkinter和PyQt都是可行的选项,但Tkinter更轻量,适合快速开发,而PyQt功能更强大,但学习曲线较陡。考虑到用户可能希望快速上手,示例可能更适合用Tkinter开始。 接下来,需要确定Python中用于串口通信的库,常用的有pyserial。因此,代码部分需要包含安装pyserial的步骤,以及如何在GUI中集成串口通信功能。同时,要处理GUI的事件循环和串口的异步读取,可能需要使用多线程或异步编程,避免界面卡顿。 然后,考虑设计一个简单的界面,包含连接/断开按钮、发送接收区域、日志显示等。每个功能模块需要分步骤实现,比如初始化串口、发送数据、接收数据、处理异常等。代码示例要简洁,但也要覆盖主要功能点,比如配置串口参数、绑定按钮事件、更新UI等。 此外,用户可能需要了解如何在实际应用中扩展功能,比如数据解析、多线程处理、错误处理等。需要给出一些优化建议,比如使用队列处理数据、添加校验机制、界面美化等。同时,引用相关的资源,比如pyserial文档、Tkinter教程,以及可能的LoRa模块手册,帮助用户进一步学习。 最后,生成相关问题,帮助用户深入理解,比如性能优化、不同GUI框架的选择、数据加密等。确保回答结构清晰,步骤明确,代码可运行,并且注意事项和扩展部分能覆盖常见问题。</think>### Python GUI实现LoRa数据通信教程 使用Tkinter实现LoRa通信包含三个核心环节:串口配置、GUI界面设计和数据交互实现。以下是分步骤实现方案: 一、环境准备 1. 安装必要库: ```bash pip install pyserial # 串口通信库[^1] pip install tkinter # 内置GUI库 ``` 二、GUI界面设计(Tkinter) ```python import tkinter as tk from serial import Serial, SerialException class LoRaGUI: def __init__(self): self.window = tk.Tk() self.window.title("LoRa通信终端 v1.0") # 串口配置区 self.com_frame = tk.LabelFrame(self.window, text="串口配置") self.com_var = tk.StringVar(value="COM3") tk.Entry(self.com_frame, textvariable=self.com_var).grid(row=0, column=0) self.connect_btn = tk.Button(self.com_frame, text="连接", command=self.toggle_connect) self.connect_btn.grid(row=0, column=1) # 数据交互区 self.data_frame = tk.LabelFrame(self.window, text="数据收发") self.send_entry = tk.Entry(self.data_frame, width=40) self.send_entry.pack() tk.Button(self.data_frame, text="发送", command=self.send_data).pack() self.recv_text = tk.Text(self.data_frame, height=10) self.recv_text.pack() # 布局 self.com_frame.pack(padx=10, pady=5) self.data_frame.pack(padx=10, pady=5) self.serial_port = None self.is_connected = False self.window.protocol("WM_DELETE_WINDOW", self.on_close) def toggle_connect(self): if not self.is_connected: try: self.serial_port = Serial( port=self.com_var.get(), baudrate=115200, timeout=1 ) self.is_connected = True self.connect_btn.config(text="断开") self.start_receive_thread() except SerialException as e: print(f"连接失败: {str(e)}") else: self.disconnect() def start_receive_thread(self): import threading self.receive_thread = threading.Thread(target=self.receive_data, daemon=True) self.receive_thread.start() def receive_data(self): while self.is_connected: try: if data := self.serial_port.readline().decode().strip(): self.recv_text.insert(tk.END, f"接收: {data}\n") self.recv_text.see(tk.END) except Exception as e: print(f"接收错误: {str(e)}") break def send_data(self): if self.is_connected: data = self.send_entry.get() + "\n" try: self.serial_port.write(data.encode()) self.recv_text.insert(tk.END, f"发送: {data.strip()}\n") except SerialException as e: print(f"发送失败: {str(e)}") def disconnect(self): self.is_connected = False if self.serial_port: self.serial_port.close() self.connect_btn.config(text="连接") def on_close(self): self.disconnect() self.window.destroy() if __name__ == "__main__": app = LoRaGUI() app.window.mainloop() ``` 三、关键实现细节 1. 串口参数配置需匹配LoRa模块设置,常用配置: - 波特率:115200/9600 - 数据位:8 - 停止位:1 - 校验位:None 2. 多线程处理: ```python # 改进后的接收线程(添加错误处理) def receive_data(self): while self.is_connected: try: if self.serial_port.in_waiting > 0: data = self.serial_port.read_all().decode() self.recv_text.insert(tk.END, f"接收: {data}\n") except (SerialException, UnicodeDecodeError) as e: self.recv_text.insert(tk.END, f"! 接收错误: {str(e)}\n") self.disconnect() ``` 四、功能扩展建议 1. 增加数据校验(CRC校验示例): ```python import binascii def crc16(data): crc = 0xFFFF for byte in data: crc ^= byte for _ in range(8): if crc & 0x0001: crc >>= 1 crc ^= 0xA001 else: crc >>= 1 return crc ``` 2. 添加通信协议(简易帧结构): ``` [HEADER][LENGTH][DATA][CRC][FOOTER] 0xAA 1字节 N字节 2字节 0x55 ``` 五、注意事项 1. 串口占用问题:确保程序关闭时释放串口资源 2. 编码一致性:发送/接收使用相同编码(推荐UTF-8) 3. 超时设置:根据实际需求调整timeout参数 4. 线程安全:避免直接跨线程操作GUI组件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值