Bg color все цвета python

Bg color все цвета python

Ряд виджетов в 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 Window Background Color

The default background color of a GUI with Tkinter is grey. You can change that to any color based on your application’s requirement.

Читайте также:  Изменить указатель мыши css

In this tutorial, we will learn how to change the background color of Tkinter window.

There are two ways through which you can change the background color of window in Tkinter. They are:

  • using configure(bg=») method of tkinter.Tk class. or
  • directly set the property bg of tkinter.Tk.

In both of the above said cases, set bg property with a valid color value. You can either provide a valid color name or a 6-digit hex value with # preceding the value, as a string.

Examples

1. Change background color using configure method

In this example, we will change the Tkinter window’s background color to blue.

Python Program

from tkinter import * gui = Tk(className='Python Examples - Window Color') # set window size gui.geometry("400x200") #set window color gui.configure(bg='blue') gui.mainloop() 

Python Tkinter Window Background Color

2. Change background color using bg property

In this example, we will change the Tkinter window’s background color to blue.

Python Program

from tkinter import * gui = Tk(className='Python Examples - Window Color') # set window size gui.geometry("400x200") #set window color gui['bg']='green' gui.mainloop()

Python Tkinter Window Background Color set using bg property

3. Using background for bg

You can use the property name bg as in the above two examples, or also use the full form background. In this example, we will use the full form background to set the background color of Tkinter window.

Python Program

from tkinter import * gui = Tk(className='Python Examples - Window Color') # set window size gui.geometry("400x200") #set window color gui['background']='yellow' #gui.configure(background='yellow') gui.mainloop() 

Python Tkinter Window Background Color

4. Using HEX code for background color

As already mentioned during the start, you can provide a HEX equivalent value for a color. In this example, we provide a HEX value to color and check the output.

Python Program

from tkinter import * gui = Tk(className='Python Examples - Window Color') # set window size gui.geometry("400x200") #set window color gui['background']='#856ff8' gui.mainloop() 

Python Tkinter Window Background Color

Summary

In this tutorial of Python Examples, we learned how to change the background color of Tkinter GUI window, with the help of well detailed Python example programs.

Источник

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.

      Источник

Оцените статью