Цвет фона окна питон

Setting Background color for Tkinter in Python

We can customize the tkinter widgets using the tkinter.ttk module. Tkinter.ttk module is used for styling the tkinter widgets such as setting the background color, foreground color, activating the buttons, adding images to labels, justifying the height and width of widgets, etc.

In order to add a background color in tkinter widgets, we can specify the background property in the widget.

Example

In the following example, we will create a button that will change the background of the text label.

#Import the tkinter library from tkinter import * from tkinter.ttk import * #Create an instance of tkinter frame win = Tk() #Set the geometry win.geometry("600x400") #Add a class to style the tkinter widgets style = ttk.Style() style.configure('TEntry', foreground = 'red') #Define a function to change the text color def change_color(): text.configure(background="red") #Create a text widget text=Label(win,text="This is a New Text",foreground="black", background="yellow",font=('Aerial bold',20)) text.pack(pady=20) #Create a Button widget Button(win, text= "Click Here", command= change_color).pack(pady=10) win.mainloop()

Output

Running the above code will create a window containing a text label with a «yellow» background color.

Читайте также:  Get all system properties in java

Now, click the «Click Here» button. It will change the background color of the text label to «Red».

Источник

Цвет фона окна Tkinter

Цвет фона по умолчанию графический интерфейс с Tkinter серый. Вы можете изменить это на любой цвет, основанный на требовании вашей заявки.

В этом руководстве мы узнаем, как изменить цвет фона окна Tkinter.

Существует два способа, через которые вы можете изменить цвет фона окна в TKinter. Они есть:

  • Использование Настроить (BG = ») Метод класса Tkinter.tk. или же
  • напрямую установить свойство BG tkinter.tk.

В обоих вышеперечисленных случаях установите свойство BG с действительным значением цвета. Вы можете предоставить действительное имя цвета или 6-значное значение шестнадцатеричное значение с # предшествующим значением, как строку.

Пример 1: Изменение цвета фона, используя метод настроителя

В этом примере мы изменим цвет фона окна Tkinter на синий.

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()

Пример 2: Изменение цвета фона с использованием свойства BG

В этом примере мы изменим цвет фона окна Tkinter на синий.

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

Пример 3: Использование фона для BG

Вы можете использовать имя недвижимости BG Как в приведенных выше двух примерах, или также используйте полную форму Фон Отказ В этом примере мы будем использовать полную форму Фон Чтобы установить цвет фона окна Tkinter.

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()

Пример 4: Использование Hex Code для цвета фона

Как уже упоминалось во время начала, вы можете предоставить шестнадцатеричное эквивалентное значение для цвета. В этом примере мы предоставляем шестнадцатеричное значение для цвета и проверяйте выходные данные.

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 GUI, с помощью хорошо подробного примера Python Python.

Читайте ещё по теме:

Источник

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.

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.

Источник

How to change a Tkinter window background color

Many candidates are rejected or down-leveled due to poor performance in their System Design Interview. Stand out in System Design Interviews and get hired in 2023 with this popular free course.

Tkinter is one of the GUI modules available in Python. It comes with the Python standard library. It is used to create cross-platform desktop GUI applications. It is lightweight and easy to use compared to other GUI modules available in Python. In this answer, we’ll learn to change the background color of the Tkinter window.

Syntax

#using configure
window.configure(bg="provide your color here")
#using bg property
window['bg'] = "provide your color here"
#using background property
window['background'] = "provide your color here"

We can specify the color with the color name or use the hex value.

Code example

Let’s look at an example below:

# Import the Tkinter library from tkinter import * # Create an instance of Tkinter window window=Tk() # Set the size of the window window.geometry("300x300") # Set the window background color window.configure(bg="red") #Uncomment below lines to use # set the window background color using bg or background property # window['bg'] = "#32a2a8" # window['background'] = "#8732a8" # Create a label widget label=Label(window, text="Hello from Educative . ").pack() window.mainloop()

Источник

Set Tkinter Background Color

Set Tkinter Background Color

  1. Method configure(background= )
  2. Attribute bg or background
  3. Tkinter Color Code

Tkinter widget class method configure and attribute bg or background could be used to set the Tkinter widget/window background color.

Method configure(background= )

try:  import Tkinter as tk except:  import tkinter as tk  app = tk.Tk() app.title("configure method") app.geometry('300x200')  app.configure(bg='red') app.mainloop() 

app.configure(bg=’red’) configures the background color of app to be red . You could also use background instead of bg .

How to set Tkinter Background Color - configure method

Attribute bg or background

background or bg is one attribute in most of Tkinter widgets, and could be used to set the background color directly.

try:  import Tkinter as tk except:  import tkinter as tk  app = tk.Tk() app.title("bg attribute") app.geometry('300x200')  app['bg'] = 'blue' app.mainloop() 

Источник

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