EmaSMach

Usando askcolor() - Tkinter Python

Jun 28th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.54 KB | None | 0 0
  1. """
  2. Ejemplo simple del uso de la funciรณn tkinter.colorchooser.askcolor()
  3. Authon: David Emanuel Sandoval.
  4. License: MIT
  5. """
  6. __author__ = "David Emanuel Sandoval"
  7. license = "MIT"
  8.  
  9. from tkinter import *
  10. from tkinter import colorchooser
  11.  
  12.  
  13. RELIEVES = ("flat", "groove", "raised", "ridge", "solid", "sunken") # Relieves de los widgets
  14.  
  15. class MyFrame(Frame):
  16.     """
  17.     Implenta un contenedor simple para mostrar unos widgets
  18.     que ejemplifican el uso de tkinter.colorchooser.askcolor().
  19.     """
  20.     def __init__(self, parent, *args, **kwargs):
  21.         super().__init__(parent, *args, **kwargs)
  22.         # Configuramos el redimensionamiento de las columnas y las filas de MyFrame
  23.         self.columnconfigure(0, weight=1)
  24.         self.rowconfigure(0, weight=1)
  25.  
  26.         self.makeWidgets()
  27.  
  28.     def makeWidgets(self):
  29.         """Crea todos los widgets."""
  30.         self.color = StringVar()
  31.         self.color.set("Color: ")
  32.  
  33.         self.frame = Frame(self, relief=RELIEVES[1], borderwidth=1)
  34.         self.frame_btn = Frame(self, relief=RELIEVES[1], borderwidth=1)
  35.  
  36.         self.label_texto = Label(self.frame, textvariable=self.color, relief=RELIEVES[3])
  37.         self.label_color = Label(self.frame, relief=RELIEVES[1])
  38.         self.btn = Button(self.frame_btn, text="Elegir color", command=self.elegir_color)
  39.  
  40.         # Posicionamos frames
  41.         self.frame.grid(row=0, column=0, pady=5, padx=5, sticky=NSEW)
  42.         self.frame_btn.grid(row=0, column=1,  pady=5, padx=5, sticky=NS)
  43.  
  44.         # Configuramos redimensionamiento de filas y columnas en self.frame
  45.         self.frame.columnconfigure(0, weight=1)
  46.         self.frame.rowconfigure(1, weight=1)
  47.  
  48.         # Posicionamos widgets
  49.         self.label_texto.grid(row=0, column=0, sticky=EW)
  50.         self.label_color.grid(row=1, column=0, sticky=NSEW)
  51.         self.btn.grid(row=0, column=0)
  52.  
  53.     def elegir_color(self):
  54.         """
  55.         Elegimos un color, guardamos el color elegido en la variable self.color,
  56.         y establecemos ese color como color de fondo del label central: self.label_color.
  57.         La funciรณn askcolor retorna una tupla, por eso selecionamos el elemento
  58.         en la posiciรณn 1, que es el color en su forma hexadecimal.
  59.         """
  60.         color = colorchooser.askcolor(title="Elija un color")
  61.         # Pasamos ese color a la variable self.color
  62.         self.color.set(f"Color: {color[1]}")
  63.         # Pasamos ese color como backgroundcolor del label
  64.         self.label_color["bg"] = color[1] # selecionamos el color en forma hexadecimal
  65.        
  66. def main():
  67.     root = Tk()
  68.     root.title("Usando tkinter.colorchooser.askcolor()")
  69.     frame = MyFrame(root)
  70.     frame.pack(expand=YES, fill=BOTH)
  71.     root.state("zoomed") # Maximizamos la ventana
  72.     root.mainloop()
  73.  
  74. if __name__ == '__main__':
  75.     main()
Advertisement
Add Comment
Please, Sign In to add comment