Not again! arggggg! object orientated programming is,.. well.... *sigh*, see here:
http://www.jpl.nu/~magnus/oo-itvt/object_orientation.html
and a piece of OOP code written in Python to show objects in action (the whole application is written as an object which could then be inherited by another object and extented upon later, etc..):
# PyHex for Windows
# A simple utility to convert RGB values to HEXIDECIMAL.
# Never explain, Never apologise, Never look back!
# Matt Malone 1996
# Imports
from Tkinter import *
import tkMessageBox
import tkColorChooser
import win32api
class mainApplication:
def __init__(self, master):
self.appName="PyHex v2.01"
self.appDoc="""A simple utility to convert RGB values to HEX.
Written by Kale.
email: sab@freeuk.com."""
self.webSafeToggle=0
master.resizable(0, 0)
master.title(self.appName)
master.bind("<Return>", self.eventconvertColour)
#master.bind("<Control_L><c>", lambda event: copyString(event))
#Menu
self.menubar = Menu(master)
self.filemenu = Menu(self.menubar, tearoff=0)
self.filemenu.add_command(label="Exit", command=master.quit)
self.menubar.add_cascade(label="File", menu=self.filemenu)
self.optionsmenu = Menu(self.menubar, tearoff=0)
self.optionsmenu.add_checkbutton(label="Web Safe", command=self.webSafe)
self.optionsmenu.add_command(label="About...", command=self.about)
self.menubar.add_cascade(label="Options", menu=self.optionsmenu)
master.config(menu=self.menubar)
# Entry field and Go button
self.frame0 = Frame(master, bd=2, relief=GROOVE)
self.frame0.pack()
self.RGBLabel=Label(self.frame0, text="RGB:").grid(row=0, column=0)
self.RField=Entry(self.frame0, relief=SUNKEN, width=3, insertofftime=150, insertontime=150)
self.RField.grid(row=0, column=1, padx=4, pady=0)
self.GField=Entry(self.frame0, relief=SUNKEN, width=3, insertofftime=150, insertontime=150)
self.GField.grid(row=0, column=2, padx=4, pady=0)
self.BField=Entry(self.frame0, relief=SUNKEN, width=3, insertofftime=150, insertontime=150)
self.BField.grid(row=0, column=3, padx=4, pady=0)
self.HEXLabel=Label(self.frame0, text="HEX:").grid(row=0, column=4)
self.HEXField=Entry(self.frame0, relief=SUNKEN, width=8, insertofftime=150, insertontime=150, takefocus=0)
self.HEXField.grid(row=0, column=5, padx=4, pady=0)
self.SpacerLabel=Label(self.frame0, text=" ").grid(row=0, column=6)
self.convertButton = Button(self.frame0, text="Convert", width=11, command=self.convertColour, takefocus=0)
self.convertButton.grid(row=0, column=7)
# Second Frame
self.frame1 = Frame(master, bd=2, relief=GROOVE)
self.frame1.pack()
self.RSlider=Scale(self.frame1, bd=1, from_=0, to=255, orient=HORIZONTAL, width=10, length=267, takefocus=0, showvalue=0, command=self.RSliderControl)
self.RSliderLabel=Label(self.frame1, text="Red:").grid(row=0, column=0)
self.RSlider.grid(row=0, column=1)
self.GSlider=Scale(self.frame1, bd=1, from_=0, to=255, orient=HORIZONTAL, width=10, length=267, takefocus=0, showvalue=0, command=self.GSliderControl)
self.GSliderLabel=Label(self.frame1, text="Green:").grid(row=1, column=0)
self.GSlider.grid(row=1, column=1)
self.BSlider=Scale(self.frame1, bd=1, from_=0, to=255, orient=HORIZONTAL, width=10, length=267, takefocus=0, showvalue=0, command=self.BSliderControl)
self.BSliderLabel=Label(self.frame1, text="Blue:").grid(row=2, column=0)
self.BSlider.grid(row=2, column=1)
# Third Frame
self.frame2 = Frame(master, bd=2, relief=GROOVE)
self.frame2.pack()
self.sampleColor=Canvas(self.frame2, bd=1, relief=SUNKEN, width=224, height=22)
self.sampleColor.pack(side=LEFT)
self.selectColourButton = Button(self.frame2, text="Select Colour", width=11, command=self.getColour, takefocus=0)
self.selectColourButton.pack(side=RIGHT)
self.initialiseRGBFields()
def about(self):
tkMessageBox.showinfo('About...',self.appName+'\n\n'+self.appDoc)
def copyString(self, event):
clipboard_clear()
clipboard_append(self.HEXField.get())
def convertColour(self):
try:
self.R=int(self.RField.get())
self.G=int(self.GField.get())
self.B=int(self.BField.get())
if self.R > 255:
self.R=255
if self.G > 255:
self.G=255
if self.B > 255:
self.B=255
self.RField.delete(0, END)
self.GField.delete(0, END)
self.BField.delete(0, END)
self.RField.insert(0, self.R)
self.GField.insert(0, self.G)
self.BField.insert(0, self.B)
self.HEXField.delete(0, END)
self.convertedColour = "#%02X%02X%02X" % (self.R, self.G, self.B)
self.HEXField.insert(0, self.convertedColour)
self.sampleColor.config(bg=self.convertedColour)
self.RSlider.set(self.R)
self.GSlider.set(self.G)
self.BSlider.set(self.B)
except ValueError:
win32api.MessageBox(0,"Please only enter numbers into the RGB fields.", self.appName, 0, 0)
self.initialiseRGBFields()
self.convertColour()
def eventconvertColour(self, event):
self.convertColour()
def getColour(self):
if len(self.HEXField.get()) > 0:
try:
self.selectedColour=tkColorChooser.askcolor(initialcolor=self.HEXField.get(), title=self.appName+" Colour Chooser")
except:
self.HEXField.delete(0, END)
self.HEXField.insert(0, "#000000")
self.selectedColour=tkColorChooser.askcolor(initialcolor=self.HEXField.get(), title=self.appName+" Colour Chooser")
else:
self.selectedColour=tkColorChooser.askcolor(title=self.appName+" Colour Chooser")
if self.selectedColour[0] != None:
self.RField.delete(0, END)
self.GField.delete(0, END)
self.BField.delete(0, END)
self.RField.insert(0, str(self.selectedColour[0][0]))
self.GField.insert(0, str(self.selectedColour[0][1]))
self.BField.insert(0, str(self.selectedColour[0][2]))
self.HEXField.delete(0, END)
self.HEXField.insert(0, "#%02X%02X%02X" % self.selectedColour[0])
self.RSlider.set(self.selectedColour[0][0])
self.GSlider.set(self.selectedColour[0][1])
self.BSlider.set(self.selectedColour[0][2])
self.sampleColor.config(bg=self.selectedColour[1])
def RSliderControl(self, event):
self.RField.delete(0, END)
self.RField.insert(0, str(self.RSlider.get()))
self.convertColour()
def GSliderControl(self, event):
self.GField.delete(0, END)
self.GField.insert(0, str(self.GSlider.get()))
self.convertColour()
def BSliderControl(self, event):
self.BField.delete(0, END)
self.BField.insert(0, str(self.BSlider.get()))
self.convertColour()
def initialiseRGBFields(self):
self.RField.delete(0, END)
self.GField.delete(0, END)
self.BField.delete(0, END)
self.RField.insert(0, 0)
self.GField.insert(0, 0)
self.BField.insert(0, 0)
self.RField.focus_set()
def webSafe(self):
if self.webSafeToggle==0:
self.webSafeToggle=1
self.RSlider.config(resolution=51)
self.GSlider.config(resolution=51)
self.BSlider.config(resolution=51)
else:
self.webSafeToggle=0
self.RSlider.config(resolution=1)
self.GSlider.config(resolution=1)
self.BSlider.config(resolution=1)
root = Tk()
app = mainApplication(root)
root.mainloop()
What the flame does not consume, consumes the flame.
------------------------------------------------------------------------
AMD XP2100+, Geforce4Ti 4400, 512Mb DDR, Abit KX7, WinXP Home