1. 程式人生 > 實用技巧 >python-圖形化介面程式設計(一個啟動停止服務的介面)

python-圖形化介面程式設計(一個啟動停止服務的介面)

GUI程式設計

1. Python實現GUI的方法

1)使用python的標準庫 Tkinter

2)使用wxPython

3)使用Jython(可以和 Java無縫整合)

2. 使用Tkinter

安裝tkinter

yuminstall-ytkintertk-devel

建立1個空白的主視窗

#!/usr/bin/python
#-*-coding:UTF-8-*-

importTkinter

#建立一個主視窗
myWindow=Tkinter.Tk()

#進入訊息迴圈
myWindow.mainloop()

寫一個服務啟動的圖形介面:

[[email protected]python]#catwin2.py
#!/usr/bin/python
#_*_coding:utf-8_*_
importos
fromTkinterimport*#匯入Tkinter庫

defrestart_server():
ip=listbox1.get(listbox1.curselection())
server=listbox2.get(listbox2.curselection())
os.system("sshpass-p'redhat'ssh
[email protected]
%sservice%srestart"%(ip,server)) print"sshpass-p'redhat'ssh[email protected]%sservice%srestart"%(ip,server) label_info["text"]="伺服器(%s)正在重啟%s服務..."%(ip,server) mywin.update() defstart_server(): ip=listbox1.get(listbox1.curselection()) server=listbox2.get(listbox2.curselection()) os.system("sshpass-p'redhat'ssh
[email protected]
%sservice%sstart"%(ip,server)) print"sshpass-p'redhat'ssh[email protected]%sservice%sstart"%(ip,server) label_info["text"]="武器器(%s)正在啟動%s服務..."%(ip,server) mywin.update() defstop_server(): ip=listbox1.get(listbox1.curselection()) server=listbox2.get(listbox2.curselection()) os.system("sshpass-p'redhat'ssh
[email protected]
%sservice%sstop"%(ip,server)) print"sshpass-p'redhat'ssh[email protected]%sservice%sstop"%(ip,server) label_info["text"]="武器器(%s)正在停止%s服務..."%(ip,server) mywin.update() mywin=Tk()#建立視窗物件的背景色 mywin.title("servermanager") 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=[] foripinrange(128,150):#第一個小部件插入資料 tmp="192.168.5.%d"%ip ips.append(tmp) foripinips: listbox1.insert(0,ip) services=["nginx","mariadb","ftp","network","httpd","mysql"] forserviceinservices: 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

7007849e412345219cbd6554b5e60ab7.png

測試直接點選啟動、停止、重啟服務:

##centos-1主機地址為192.168.5.129,且httpd服務沒啟動:

[[email protected] ~]# service httpd status

httpd 已停

在圖形介面點選192.168.5.129àhttpdà啟動服務

a2e01c19cc3b1b61c115c5c753c378cc.png

centos-1機器上檢視httpd的狀態:

[[email protected] ~]# service httpd status

httpd (pid5206) 正在執行...

在點選停止服務:

0037b01666d409ba1c6923f3f48e4f2d.png

檢視centos-1主機httpd狀態:

[[email protected] ~]# service httpd status

httpd 已停

點選重啟:

4082aa7d3d689414577f895153705b9a.png

檢視centos-1主機httpd狀態:

[[email protected] ~]# service httpd status

httpd (pid5635) 正在執行...


轉載於:https://blog.51cto.com/legehappy/1983279