Python tkinter таблица цветов

Python Tkinter Colors + Example

In this Python tutorial, we will learn how to implement colors using Python Tkinter. Let us understand in detail Python Tkinter colors with the points:

  • Python Tkinter Colors
  • Python Tkinter Colors RGB
  • Python Tkinter Color Text
  • Python Tkinter Color Button
  • Python Tkinter Color Chooser
  • Python Tkinter Color Label
  • Python Tkinter Color Transparent
  • Python Tkinter Color Window

Python Tkinter Colors

Colors play an important role in the UI/UX of the application. It adds a professional touch to the application and helps in engaging the user for a longer time.

  • Every color has some thought behind it, selecting the right color for an application is an art & it takes time to master it.
  • Red symbolizes an error, green means okay or done, yellow means something is incomplete, etc.
  • Every color has two things and the developer/user can provide either of them:
    • color name: ‘white smoke’, ‘red’, ‘floral white’, ‘blue’, etc
    • color code: #F5F5F5, #FF0000, #FFFAF0, 0000FF, etc

    Python Tkinter Colors RGB

    Python Tkinter does not support input of colors in RGB format but we can create a workaround that accepts inputs in a RGB format.

    • In the below program, you can notice that we have created a function that accepts user input and it adds pound sign (#) as prefix and byte code as a suffix.
    • Now while providing the background color we have used this function and provided the RGB values.
    from tkinter import * def rgb_hack(rgb): return "#%02x%02x%02x" % rgb ws = Tk() ws.title('PythonGuides') ws.geometry('400x300') ws.config(bg=rgb_hack((255, 0, 122))) ws.mainloop()

    In this output, you can see that window has a color passed in the code in a RGB format.

    python tkinter colors rgb

    Python Tkinter Color Text

    In this section, we will learn how to set the color of the Text in Python Tkinter.

    • foreground or fg is the option that accepts the color input from the user and sets the font or text color.
    • This option is common in almost all the widgets and can be applied on the text of any widget.
    • In our example, we will be implementing color on the Text box widget. By default the Text color is blue but we will change it to Blue.
    from tkinter import * ws = Tk() ws.title('PythonGuides') ws.config(bg='#D9D8D7') ws.geometry('400x300') tb = Text( ws, width=25, height=8, font=('Times', 20), wrap='word', fg='#4A7A8C' ) tb.pack(expand=True) ws.mainloop()

    Output of Python Tkinter Color Text

    In this output, we have changed the default color of the text from black to light blue.

    python tkinter color text

    Python Tkinter Color Button

    In this section, we will learn how to change the color of the Button and button text in Python Tkinter.

    • Button widget in Python Tkinter has mainly three colors applied on it.
      • Button Text Color
      • Button background Color
      • Button color when clicked
      from tkinter import * ws = Tk() ws.titlSelect colorSelect colore('PythonGuides') ws.config(bg='#D9D8D7') ws.geometry('400x300') Button( ws, text='Download', font=('Times', 20), padx=10, pady=10, bg='#4a7abc', fg='yellow', activebackground='green', activeforeground='white' ).pack(expand=True) ws.mainloop()

      In this output, a button has a background color as light-blue and yellow text. When the mouse is hovered-over it, the button changes to a green background and white foreground or text color.

      Python Tkinter Color Chooser

      In this section, we will learn to create a color chooser using python tkinter.

      • Python Tkinter provides colorchooser module using which color toolbox can be displayed.
      • Color chooser allows users to choose colors from the color tray.
      • colorchooser returns the RGB code with hexadecimal color code in the tuple format.
      • In our example, we will create an application that allows users to choose any color from the color tray, and then that color will be displayed on the label.
      from tkinter import * from tkinter import colorchooser def pick_color(): color = colorchooser.askcolor(title ="Choose color") color_me.config(bg=color[1]) color_me.config(text=color) ws = Tk() ws.title('PythonGuides') ws.geometry('400x300') color_me = Label( ws, text='(217, 217, 217) #d9d9d9', font = ('Times', 20), relief = SOLID, padx=20, pady=20 ) color_me.pack(expand=True) button = Button( ws, text = "Choose Color", command = pick_color, padx=10, pady=10, font=('Times', 18), bg='#4a7a8c' ) button.pack() ws.mainloop() 

      In this output, you can see that when the user clicks on the choose color button then a color tray is pop-up. and the label changes color to the selected color.

      This is how to use a color chooser in Python Tkinter.

      Python Tkinter Color Label

      In this section, we will learn how to change color of a Label widget in Python Tkinter.

      • Label in Python Tkinter is a widget that is used to display text and images on the application.
      • We can apply color on the Label widget and Label Text.
      • To color the widget label, the background or bg keyword is used, and to change the text color of the label widget, the foreground or fg keyword is used.
      • In this example, we have a colored label widget and label text.
      from tkinter import * ws = Tk() ws.title('PythonGuides') ws.config(bg='#D9D8D7') ws.geometry('400x300') Label( ws, text='This text is displayed \n using Label Widget', font=('Times', 20), padx=10, pady=10, bg='#4a7abc', fg='yellow' ).pack(expand=True) ws.mainloop()

      In this output, Label widget is painted with light blue and Label text with yellow

      Python Tkinter Color Label

      Python Tkinter Color Transparent

      In this section, we will learn how to set the color to transparent in Python Tkinter. In other words, removing all the colors from the background so that background things start appearing through the window.

      • The first step in the process is to set the background color to the window using the config keyword.
      • now provide the same background color in the wm_attributes()
      • Here is the implementation of Python Tkinter Color Transparent.
      from tkinter import * ws = Tk() ws.title('PythonGuides') ws.geometry('400x300') ws.config(bg='#4a7a8c') ws.wm_attributes('-transparentcolor','#4a7a8c') ws.mainloop()

      Here is the output of Python Tkinter Color Transparent

      In this output, the application is transparent and the color you are seeing is the background color of the desktop.

      Python Tkinter Color Transparent

      Python Tkinter Color Window

      In this section, we will learn how to color the main window of Python Tkinter.

      • In Python Tkinter, we can change the default settings of the widget by changing the configuration.
      • To change the configuration we can use the keyword config or configure.
      from tkinter import * ws = Tk() ws.title('PythonGuides') ws.geometry('400x300') ws.config(bg='#116562') ws.mainloop()

      In this output, window color is changed displayed. Default color of Python Tkinter window is Light Gray, but we have changed it to see green.

      python tkinter color window

      You may like the following Python Tkinter tutorials:

      In this tutorial, we have learned how to use color in Python Tkinter. Also, we have covered these topics.

      • Python Tkinter Colors
      • Python Tkinter Colors Example
      • Python Tkinter Colors RGB
      • Python Tkinter Color Text
      • Python Tkinter Color Button
      • Python Tkinter Color Chooser
      • Python Tkinter Color Label
      • Python Tkinter Color Transparent
      • Python Tkinter Color Window

      I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.

      Источник

      Python tkinter таблица цветов

      Ряд виджетов в Tkinter поддерживают установку цвета для различных аспектов. Например, у виджета Label можно установить параметры foreground и background , которые отвечают за цвет текста и фона соответственно. У некоторых виджетов настройки цвета спрятаны в параметре style.

      Цвет можно установить разными способами:

        Именнованные цвета, например, «red», который соответствует красному цвету. В зависимости от платформы набор доступных именнованных цветов может отличаться. Все доступные именнованные цвета можно посмотреть в документации. Например:

      ttk.Label(text="Hello World", foreground="red")
      from tkinter import * from tkinter import ttk root = Tk() root.title("METANIT.COM") root.geometry("250x200") label = ttk.Label(text="Hello World", padding=8, foreground="#01579B", background="#B3E5FC") label.pack(anchor=CENTER, expand=1) root.mainloop()

      Установка цвета в Tkinter и Python

    Если нам даны отдельные коды RGB-составляющих, то их можно сконвертировать в шестнадцатеричный код цвета:

    from tkinter import * from tkinter import ttk root = Tk() root.title("METANIT.COM") root.geometry("250x200") def get_rgb(rgb): return "#%02x%02x%02x" % rgb label = ttk.Label(text="Hello World", padding=8, foreground=get_rgb((0, 77, 64)), background=get_rgb((128, 203, 196))) label.pack(anchor=CENTER, expand=1) root.mainloop()

    Здесь функция get_rgb в качестве параметра получает кортеж из трех составляющих цвет RGB и с помощью форматирования строки переводит значения кортежа в шестнадцатеричный код

    Источник

    Tkinter Colors – A Complete Guide

    Tkinter Colours Cover Image

    Tkinter is an inbuilt module available in Python for developing Graphical User Interfaces (GUI). It allows us to develop desktop applications. Tkinter is very simple and easy to work with. It provides us with different widgets like button, canvas, label, menu, message, etc. for building the GUIs in Python.

    You can explore all of our Tkinter tutorials as well! This tutorial will introduce you to the use of different colour options in Tkinter.

    What are Colors in Tkinter?

    Colours are mainly used for making the GUI attractive. Tkinter treats colours as strings. Colours can be mentioned in two ways:

    1. Hexadecimal values
      Ex. #FF0000 (Red), #008000 (Green), #FFFF00 (Yellow), etc.
    2. Colour Name
      Ex. Gold, Silver, Blue, etc.

    Here is a list of colour codes for quick reference:

    Tkinter Colour List

    Now, let’s explore the different colour options available to us in Tkinter.

    Colour Options in Tkinter

    First, let’s see the general structure of the GUI that we will be using to explore all the colour options.

    import tkinter as tk from tkinter import * #main window root = tk.Tk() #title of the window root.title("Tkinter Colors") #disabling resizing of the window root.resizable(0, 0) #---frame for top name--- top = Frame(root, width = 500, height = 70, bd=8, relief="raise") top.pack(side = TOP) #--name in the frame-- name = Label(top, text="Colour Option Name", width=30, height=2, font = ("Lucida Console", 14, "italic")) name.grid(padx = 18) #--button-- button = Button(top, text="Click Here", width=20, height=3) button.grid(padx = 2) root.mainloop()

    Tkinter Colour GUI Structure

    The above is a basic GUI having the title ‘Tkinter Colors’. It consists of a frame at the top which will display the colour choice name and a button below it that will be used to demonstrate the functionality.

    An element or widget is an active element if pointing the cursor on it and pressing the mouse button it will perform some action.

    1. activebackground

    We can use this option on an active element to set the background colour of the widget when the widget is active.

    import tkinter as tk from tkinter import * #main window root = tk.Tk() #title of the window root.title("Tkinter Colors") #disabling resizing of the window root.resizable(0, 0) #---frame for top name--- top = Frame(root, width = 500, height = 70, bd=8, relief="raise") top.pack(side = TOP) #--name in the frame-- name = Label(top, text="Active Background", width=30, height=2, font = ("Lucida Console", 14, "italic")) name.grid(padx = 18) #--button-- button = Button(top, text="Click Here", width=20, height=3, activebackground='red') button.grid(padx = 2) root.mainloop()

    Tkinter Background Colour

    The background of the label is yellow in colour here.

    4. foreground

    This colour denotes the foreground colour of a widget. It can also be used for both active and inactive elements. The foreground can also be specified as fg . Here as well, instead of a button, we have used a label as a widget to demonstrate the option.

    import tkinter as tk from tkinter import * #main window root = tk.Tk() #title of the window root.title("Tkinter Colors") #disabling resizing of the window root.resizable(0, 0) #---frame for top name--- top = Frame(root, width = 500, height = 70, bd=8, relief="raise") top.pack(side = TOP) #--name in the frame-- name = Label(top, text="Foreground", width=30, height=2, font = ("Andalus", 14)) name.grid(padx = 18) #--label-- label = Label(top, text="Label foreground", width=20, height=3, borderwidth=5, relief="solid", font = ("Lucida Console", 12, "bold"), foreground='blue') label.grid(padx = 2) root.mainloop()

    Tkinter Foreground Colour

    As we see, the foreground colour of the label is blue here.

    Conclusion

    That’s it for this tutorial. We have learnt about the different colour options available in Tkinter.

    Источник

    Читайте также:  Размер текста
Оцените статью