python-graphical interface programming (an interface to start and stop the service)

Source: Internet
Author: User

GuiProgramming1. PythonImplementGuithe method

1) use python 's standard library Tkinter

2) use WxPython

3) use Jython(can be seamlessly integrated with Java)

2.UseTkinter

installing the Tkinter library

Yum install-y tkinter Tk-devel

Create 1 blank main window

#!/usr/bin/python#-*-coding:utf-8-*-Import Tkinter # Create a main window Mywindow = tkinter.tk () # Enter message loop Mywindow.mainloop ()

Write a service-initiated graphical interface:

[[email protected] python]# cat win2.py#!/usr/bin/python#_*_coding:utf-8_*_import  osfrom tkinter import *                         #  Import  Tkinter  Library  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"]= "server (%s) is being re-Start%s service ... "% (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 "]=" Weapon device (%s Starting%s service ... "% (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"]= "Weapon device (%s) stopping%s service ..."% (ip,server)         mywin.update ()  mywin = tk ()                      #  Create a Window object's background color  mywin.title ("server  Manager ") Mywin.geometry (" 700x400 ")   #创建标签label1  = label (mywin,text=" Server List ", font= (" Arial "), bg= ' Yellow ', fg= ' Red ') Label1.grid (row=0,column=0) Label2 = label (mywin,text= "service Name", font= ("Arial", +), bg= ' Blue ', fg= ' Red ') Label2.grid (rOw=0,column=1) Label3 = label (mywin,text= "Operation", Font= ("Arial", "a"), bg= "Blue", fg= "Red") Label3.grid (row=0, column=2,columnspan=4) Label_info =label (mywin,text= "", Font= ("Arial",), bg= ' Purple ', fg= ' red ') label_ Info.grid (row=2,column=0)   #创建列表组件listbox1  =listbox (mywin,exportselection=false) listbox2 = ListBox (Mywin,exportselection=false) #exportselection =false, yes Select a different ListBox, the original selection is still valid   #创建按钮button1  =  button (mywin,text= "Restart Service", Command=restart_server) Button2 = button (mywin,text= "Start Service", command= Start_server) Button3 = button (mywin,text= "Stop Service", Command=stop_server)   #给列表组件添加数据ips  =  []for ip in range (128,150):                             #  First widget Insert data          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)               #  Second widget Insert data  # To the ListBox layout 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 ()                   #  entering the message loop


execution of win2.py:

[email protected] python]# python win2.py

650) this.width=650; "src=" Https://s2.51cto.com/oss/201711/19/7007849e412345219cbd6554b5e60ab7.png "title=" 1.png " Width= "height=" 341 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:700px;height:341px; "alt=" 7007849e412345219cbd6554b5e60ab7.png "/>

Test Direct Click to start, stop, restart the service:

# #centos-1 The host address is 192.168.5.129 andthe httpd service does not start:

[[Email protected] ~]# service httpd status

HTTPD has stopped

in the graphical interface click 192.168.5.129àhttpdà start service

650) this.width=650; "src=" Https://s4.51cto.com/oss/201711/19/a2e01c19cc3b1b61c115c5c753c378cc.png "title=" 2.png " Width= "height=" 467 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:700PX;HEIGHT:467PX; "alt=" A2e01c19cc3b1b61c115c5c753c378cc.png "/>

View the status of httpd on the centos-1 machine :

[[Email protected] ~]# service httpd status

HTTPD (PID 5206) is running ...

In the Click Stop Service:

650) this.width=650; "src=" Https://s2.51cto.com/oss/201711/19/0037b01666d409ba1c6923f3f48e4f2d.png "title=" 3.png " Width= "height=" 461 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" width:700px;height:461px; "alt=" 0037b01666d409ba1c6923f3f48e4f2d.png "/>

To view the centos-1 host httpd status:

[[Email protected] ~]# service httpd status

HTTPD has stopped

Click Restart:

650) this.width=650; "src=" Https://s3.51cto.com/oss/201711/19/4082aa7d3d689414577f895153705b9a.png "title=" 4.png " Width= "height=" 467 "border=" 0 "hspace=" 0 "vspace=" 0 "style=" WIDTH:700PX;HEIGHT:467PX; "alt=" 4082aa7d3d689414577f895153705b9a.png "/>

To view the centos-1 host httpd status:

[[Email protected] ~]# service httpd status

HTTPD (PID 5635) is running ...


This article is from the "First Legehappy51cto blog" blog, be sure to keep this source http://legehappy.blog.51cto.com/13251607/1983279

python-graphical interface programming (an interface to start and stop the service)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.