Akemi

Python-tk库—图形化管理nginx服务

2024/12/01

tkinter库是python标准的GUI库,提供窗口、按钮、标签、菜单、框架等GUI功能

易于使用、并且可以在不同操作系统上运行

tkinter库是自带的,但额外需要一个模块pyinstall,这个模块可以将python代码打包成exe程序

pip3 install pyinstaller

tkinter基本组件
窗口
标签
按钮
文本框
框架
菜单

心得

在linux中,可以通过update-alternatives –config来切换默认环境
但如果涉及多环境管理,就需要使用venv这样的虚拟环境

在windows中,同样的可以通过PyCharm这样的工具来管理不同解释器环境
但如果需要运行一些安装好模块的可执行文件,比如pyinstaller,如果windows的python环境变量位置与模块的不同,就需要通过手动执行pyinstaller.exe的方式进行打包

这个问题搞了我一天,幸好最后还是搞明白了

案例-管理nginx

简单思路:使用paramiko远程nginx服务器、定义各种按钮,定义执行命令函数,
通过执行不同命令来实现按钮功能

并且通过创建子窗口来查看日志

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import tkinter as tk
from tkinter import messagebox
import paramiko

HOST='192.168.10.114'
USERNAME='root'
PASSWORD='1'

def run_system_command(command):
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh.connect(HOST,username=USERNAME,password=PASSWORD)
stdin,stdout,stderr=ssh.exec_command(command)
output=stdout.read().decode('utf-8')
error=stderr.read().decode('utf-8')
return output.strip(),error.strip()
except Exception as e:
return "",str(e)
finally:
ssh.close()

def get_nginx_status():
output,error=run_system_command("systemctl is-active nginx")
# 如果error有值
if error:
return "未知"
return output.strip()

# 更新nginx状态的标签
def update_status_label():
status=get_nginx_status()
status_label.config(text=f"nginx状态:{status}")
root.after(5000,update_status_label) # 5000毫秒更新一次标签的状态
# 启动nginx服务
def start_nginx():
output,error=run_system_command('systemctl start nginx')
if error:
messagebox.showerror("错误",f"启动nginx失败:{error}")
else:
messagebox.showinfo("成功","nginx已启动成功")
update_status_label()

def stop_nginx():
output,error=run_system_command('systemctl stop nginx')
if error:
messagebox.showerror("错误",f"停止nginx失败:{error}")
else:
messagebox.showinfo("成功","nginx已停止成功")
update_status_label()

def restart_nginx():
output,error=run_system_command('systemctl restart nginx')
if error:
messagebox.showerror("错误",f"重启nginx失败:{error}")
else:
messagebox.showinfo("成功","nginx已重启成功")
update_status_label()

# 查看nginx日志
def view_nginx_logs():
output,error=run_system_command('tail -n 100 /var/log/nginx/access.log')
if error:
messagebox.showerror("错误",f"查看日志失败:{error}")
else:
#创建子窗口
logs_window=tk.Toplevel(root)
logs_window.title('nginx日志')
logs_text=tk.Text(logs_window,wrap='word') #文本内容过长自动换行
logs_text.insert(tk.END,output)#把内容插入到文本框末尾
logs_text.pack(fill='both',expand=True) #文本框内容随框大小变化自动扩展

# 创建GUI主窗口
root=tk.Tk()
#设置主窗口标题
root.title("nginx管理工具")

#设置初始化nginx状态的标签、字体
status_label=tk.Label(root,text="nginx状态:正在检测",font=('Arial',12))
#设置标签的布局位置,pack左右,pady上下
status_label.pack(padx=10,pady=10)

#创建按钮——关联到函数
#启动按钮
start_button=tk.Button(root,text="启动nginx",command=start_nginx)
start_button.pack(padx=10,pady=10)

#停止按钮
stop_button=tk.Button(root,text="停止nginx",command=stop_nginx)
stop_button.pack(padx=10,pady=10)

#重启按钮
restart_button=tk.Button(root,text="重启nginx",command=restart_nginx)
restart_button.pack(padx=10,pady=10)

#查看nginx日志按钮
logs_button=tk.Button(root,text="查看nginx日志",command=view_nginx_logs)
logs_button.pack(padx=10,pady=10)
update_status_label()

#运行主窗口
root.mainloop()

编译,生成一个exe文件

进入windows的cmd命令行

1
2
3
4
5
6
7
8
9
10
pip3 install pyinstaller
#进入文件所在目录
cd C:\Users\13209\PycharmProjects\project01

pyinstaller --onefile --windowed ".\166 tkinter库-.py"
或者指定要使用的pyinstaller.exe
C:\Python\Python37\Scripts\pyinstaller.exe --onefile --windowed ".\166 tkinter 库-.py""

#编译成功提示
#8382 INFO: Building EXE from EXE-00.toc completed successfully.

CATALOG
  1. 1. 案例-管理nginx