Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Ejemplo simple del uso de la funciรณn tkinter.colorchooser.askcolor()
- Authon: David Emanuel Sandoval.
- License: MIT
- """
- __author__ = "David Emanuel Sandoval"
- license = "MIT"
- from tkinter import *
- from tkinter import colorchooser
- RELIEVES = ("flat", "groove", "raised", "ridge", "solid", "sunken") # Relieves de los widgets
- class MyFrame(Frame):
- """
- Implenta un contenedor simple para mostrar unos widgets
- que ejemplifican el uso de tkinter.colorchooser.askcolor().
- """
- def __init__(self, parent, *args, **kwargs):
- super().__init__(parent, *args, **kwargs)
- # Configuramos el redimensionamiento de las columnas y las filas de MyFrame
- self.columnconfigure(0, weight=1)
- self.rowconfigure(0, weight=1)
- self.makeWidgets()
- def makeWidgets(self):
- """Crea todos los widgets."""
- self.color = StringVar()
- self.color.set("Color: ")
- self.frame = Frame(self, relief=RELIEVES[1], borderwidth=1)
- self.frame_btn = Frame(self, relief=RELIEVES[1], borderwidth=1)
- self.label_texto = Label(self.frame, textvariable=self.color, relief=RELIEVES[3])
- self.label_color = Label(self.frame, relief=RELIEVES[1])
- self.btn = Button(self.frame_btn, text="Elegir color", command=self.elegir_color)
- # Posicionamos frames
- self.frame.grid(row=0, column=0, pady=5, padx=5, sticky=NSEW)
- self.frame_btn.grid(row=0, column=1, pady=5, padx=5, sticky=NS)
- # Configuramos redimensionamiento de filas y columnas en self.frame
- self.frame.columnconfigure(0, weight=1)
- self.frame.rowconfigure(1, weight=1)
- # Posicionamos widgets
- self.label_texto.grid(row=0, column=0, sticky=EW)
- self.label_color.grid(row=1, column=0, sticky=NSEW)
- self.btn.grid(row=0, column=0)
- def elegir_color(self):
- """
- Elegimos un color, guardamos el color elegido en la variable self.color,
- y establecemos ese color como color de fondo del label central: self.label_color.
- La funciรณn askcolor retorna una tupla, por eso selecionamos el elemento
- en la posiciรณn 1, que es el color en su forma hexadecimal.
- """
- color = colorchooser.askcolor(title="Elija un color")
- # Pasamos ese color a la variable self.color
- self.color.set(f"Color: {color[1]}")
- # Pasamos ese color como backgroundcolor del label
- self.label_color["bg"] = color[1] # selecionamos el color en forma hexadecimal
- def main():
- root = Tk()
- root.title("Usando tkinter.colorchooser.askcolor()")
- frame = MyFrame(root)
- frame.pack(expand=YES, fill=BOTH)
- root.state("zoomed") # Maximizamos la ventana
- root.mainloop()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment