GUI编程
1. Python实现GUI的方法
1)使用python的标准库 Tkinter
2)使用wxPython
3)使用Jython(可以和 Java无缝集成)
2. 使用Tkinter
安装tkinter库
yum install -y tkinter tk-devel
创建1个空白的主窗口
#!/usr/bin/python # -*- coding: UTF-8 -*- import Tkinter # 创建一个主窗口 myWindow = Tkinter.Tk() # 进入消息循环 myWindow.mainloop()
写一个服务启动的图形界面:
[[email protected] python]# cat win2.py #!/usr/bin/python #_*_coding:utf-8_*_ import os from Tkinter import * # 导入 Tkinter 库 def restart_server(): ip= listbox1.get(listbox1.curselection()) server= listbox2.get(listbox2.curselection()) os.system("sshpass-p ‘redhat‘ ssh [email protected]%s service %s restart" %(ip,server)) print"sshpass -p ‘redhat‘ ssh [email protected]%s service %s restart" %(ip,server) label_info["text"]="服务器(%s)正在重启%s服务..."%(ip,server) mywin.update() def start_server(): ip= listbox1.get(listbox1.curselection()) server= listbox2.get(listbox2.curselection()) os.system("sshpass-p ‘redhat‘ ssh [email protected]%s service %s start" %(ip,server)) print"sshpass -p ‘redhat‘ ssh [email protected]%s service %s start" %(ip,server) label_info["text"]="武器器(%s)正在启动%s服务..."%(ip,server) mywin.update() def stop_server(): ip = listbox1.get(listbox1.curselection()) server = listbox2.get(listbox2.curselection()) os.system("sshpass -p ‘redhat‘ ssh [email protected]%s service %s stop"%(ip,server)) print "sshpass -p ‘redhat‘ ssh [email protected]%s service %s stop"%(ip,server) label_info["text"]="武器器(%s)正在停止%s服务..."%(ip,server) mywin.update() mywin = Tk() # 创建窗口对象的背景色 mywin.title("server manager") mywin.geometry("700x400") #创建标签 label1 = Label(mywin,text="服务器列表",font=("Arial",20),bg=‘yellow‘,fg=‘red‘) label1.grid(row=0,column=0) label2 = Label(mywin,text="服务名称",font=("Arial",20),bg=‘blue‘,fg=‘red‘) label2.grid(row=0,column=1) label3 = Label(mywin,text="操作",font=("Arial",20),bg="blue",fg="red") label3.grid(row=0,column=2,columnspan=4) label_info =Label(mywin,text="",font=("Arial",20),bg=‘purple‘,fg=‘red‘) label_info.grid(row=2,column=0) #创建列表组件 listbox1 =Listbox(mywin,exportselection=False) listbox2 =Listbox(mywin,exportselection=False) #exportselection=False,是的选择其他的Listbox时,原来的选择依然有效 #创建按钮 button1 = Button(mywin,text="重启服务",command=restart_server) button2 = Button(mywin,text="启动服务",command=start_server) button3 = Button(mywin,text="停止服务",command=stop_server) #给列表组件添加数据 ips = [] for ip in range(128,150): # 第一个小部件插入数据 tmp= "192.168.5.%d" %ip ips.append(tmp) for ip in ips: listbox1.insert(0,ip) services =["nginx","mariadb","ftp","network","httpd","mysql"] for service in services: listbox2.insert(0,service) # 第二个小部件插入数据 #给listbox布局 listbox1.grid(row=1,column=0,padx=20,pady=20,ipady=10) listbox2.grid(row=1,column=1,padx=20,pady=20) button1.grid(row=1,column=2,padx=10,pady=10) button2.grid(row=1,column=3,padx=10,pady=10) button3.grid(row=1,column=4,padx=10,pady=10) mywin.mainloop() # 进入消息循环
执行win2.py的效果图:
[[email protected] python]# python win2.py
测试直接点击启动、停止、重启服务:
##centos-1主机地址为192.168.5.129,且httpd服务没启动:
[[email protected] ~]# service httpd status
httpd 已停
在图形界面点击192.168.5.129àhttpdà启动服务
在centos-1机器上查看httpd的状态:
[[email protected] ~]# service httpd status
httpd (pid 5206) 正在运行...
在点击停止服务:
查看centos-1主机httpd状态:
[[email protected] ~]# service httpd status
httpd 已停
点击重启:
查看centos-1主机httpd状态:
[[email protected] ~]# service httpd status
httpd (pid 5635) 正在运行...
时间: 2024-11-02 20:06:54