Below simple python application invoke a GUI with multiple color buttons and once you click on the button it will open a xterm with selected color .
As you can see in below image first row is host name and font color and second row is
host load ( enter host name in first entry box and press enter and then you will see the load of the host in second row ).
Third row to final row is color buttons , once you click on the button it will open a xterm with selected button color .
By default application using "ssh" for login ,"uptime" command to get load of the machine and "showrgb" command for colors .
Reference :
#!/usr/bin/env python
# -*- coding: iso-8859-1 -*-
import Tkinter
class AutoScrollbar(Tkinter.Scrollbar):
def set(self, lo, hi):
if float(lo) <= 0.0 and float(hi) >= 1.0:
self.tk.call("grid", "remove", self)
else:
self.grid()
Tkinter.Scrollbar.set(self, lo, hi)
class simpleapp_tk(Tkinter.Tk):
def __init__(self,parent):
Tkinter.Tk.__init__(self,parent)
self.parent = parent
self.initialize()
def initialize(self):
import os
import Tkinter
self.grid()
#host and name entry
self.entryVariable = Tkinter.StringVar()
self.entry = Tkinter.Entry(self,textvariable=self.entryVariable,width=14)
self.label = Tkinter.Label(self,text="Enter Host Name" )
self.label.grid(row=0, column=0)
self.entry.grid(row=0, column=1)
self.entry.bind("<Return>", self.OnPressEnter)
self.entryVariable.set(os.environ['HOST'])
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
#font color
self.entryVariable2 = Tkinter.StringVar()
self.entry2 = Tkinter.Entry(self,textvariable=self.entryVariable2,width=12)
self.label2 = Tkinter.Label(self,text=" font color" )
self.label2.grid(row=0, column=4)
self.entry2.grid(row=0,column=5,sticky='EW')
self.entryVariable2.set("black")
self.entry2.bind("<Return>", self.OnPressEnter)
self.entry2.focus_set()
self.entry2.selection_range(0, Tkinter.END)
self.button = Tkinter.Button(self, text="QUIT", command=self.quit)
self.button.grid(row=0,column=6)
#machine load
self.labelVariable = Tkinter.StringVar()
label = Tkinter.Label(self,textvariable=self.labelVariable, anchor="w",bg="DarkGray")
label.grid(row=1,column=0,columnspan=7)
machine = self.entryVariable.get()
p = os.popen("uptime")
#p = os.popen('rup %s ' % machine)
macload = p.readline()
p.close()
self.labelVariable.set(macload)
#get xterm colors
rgbcolors = os.popen('showrgb ').read()
rgbcolors = rgbcolors.replace(" ","")
rgbcolors = rgbcolors.splitlines()
rgbcolorslist = []
rgbcolorslist = list()
for r in rgbcolors :
rgbcolorslist.append(r.split("\t\t",1)[1])
#Scroll
vscrollbar = AutoScrollbar(self)
vscrollbar.grid(row=2, column=7, sticky=Tkinter.N+Tkinter.S)
hscrollbar = AutoScrollbar(self, orient=Tkinter.VERTICAL)
hscrollbar.grid(row=3, column=0, sticky=Tkinter.E+Tkinter.W)
canvas = Tkinter.Canvas(self, yscrollcommand=vscrollbar.set, xscrollcommand=hscrollbar.set)
canvas.grid(row=2, column=0, columnspan=7)
vscrollbar.config(command=canvas.yview)
hscrollbar.config(command=canvas.xview)
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
frame = Tkinter.Frame(canvas)
frame.rowconfigure(0, weight=1)
frame.columnconfigure(0, weight=1)
#colors buttons
r = 0
c = 0
fgcolor = self.entryVariable2.get()
ncolor = ""
if fgcolor == ncolor :
fgcolor = "black"
for index in rgbcolorslist :
self.button = Tkinter.Button(frame, text=index,bg=index,fg="black",width=12,command=lambda i=index: self.OnButtonClick(i,fgcolor) )
if c == 6 :
c = 0
r = r+1
else :
self.button.grid(column=c,row=6+r)
c = c+1
canvas.create_window(0, 0, anchor=Tkinter.NW, window=frame)
frame.update_idletasks()
canvas.config(scrollregion=canvas.bbox("all"),width=697)
self.resizable(True,False)
self.update()
self.geometry(self.geometry())
def OnButtonClick(self,bcolor,fcolor):
import os
machine = self.entryVariable.get()
fcolor = self.entryVariable2.get()
ncolor = ""
if fcolor == ncolor :
fcolor = "black"
os.system('ssh -n %s "setenv DISPLAY $DISPLAY;setenv HOSTNAME $HOST ; xterm -bg %s -fg %s " &' % (machine,bcolor,fcolor))
self.labelVariable.set( "Opening xterm on "+ machine )
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
def OnPressEnter(self,event):
import os
machine = self.entryVariable.get()
p = os.popen('ssh -n %s "uptime"' % machine)
#p = os.popen('rup %s ' % machine)
macload = p.readline()
p.close()
self.labelVariable.set( macload )
self.entry.focus_set()
self.entry.selection_range(0, Tkinter.END)
fgcolor = self.entryVariable2.get()
ncolor = ""
if fgcolor == ncolor :
fgcolor = "black"
self.entry2.focus_set()
self.entry2.selection_range(0, Tkinter.END)
if __name__ == "__main__":
app = simpleapp_tk(None)
app.title('colors xterm ')
app.mainloop()
No comments:
Post a Comment