Background Color for Tk in Python

Background color for Tk in Python

root.configure(background='black')

or more generally

<widget>.configure(background='black')

Python tkinter change background color with a button

I fixed your code by using the .config() function. In the background changing function, you don't attempt to change the background. You only change a StringVar(), which doesn't change the background anyways.

I also made your gui look better, like so:

import tkinter
background = "white"
okno = tkinter.Tk()
okno.title("Project")
okno.config(bg = "white")
platno = tkinter.Canvas(okno, height = 300, width = 300, bg = background, highlightthickness = 0)
platno.pack()
def background_color():
background = vstup2.get()
try:
platno.config(bg = background)
except:
pass

tkinter.Label(okno,text = "Background color :", bg = "white", width = 30).pack()
vstup2 = tkinter.StringVar()
tkinter.Entry(okno,textvariable = vstup2, bg = "white").pack()
tkinter.Button(okno,width=30, text="Set the color of a background", command=background_color, relief = "flat", activebackground = "white", bd = 0, bg = "white").pack()

okno.mainloop()

Output:
enter image description here

You also have to add a .mainloop() at the end. In some text editors, if you don't add that, the program wont run properly.

Hope this helps!

How to change my background color with tkinter?

Your panel label is taking all the space of the root window. So to change the bg color, config its background colour instead.

panel = tk.Label(root, image=img, bg="black")

Change background color in python using tkinter

In the for loop for frames, add a line for backgrounds:

for F in (StartPage, Departure, PageTwo):
page_name = F.__name__
frame = F(parent=container, controller=self)
self.frames[page_name] = frame
frame.config(bg="black") # or whatever color you like

Setting background color or bg for a button in Python Tkinter not working

Unfortunately, there isn't an easy way to change the background of a button from the ttk library.

But you can easily get what you want with a normal tkinter. Button if you set the right options. Below is an example script:

from tkinter import *
from tkinter import ttk

root = Tk()
frame = Frame(root).grid(row = 0, column = 0)

button = Button(frame, text = "CLICK ME", bg = '#05752a').grid(row = 0, column = 0)

root.mainloop()

This is the Link to another post

Change Background Tkinter in Classes

Try this:

import tkinter as tk
import sqlite3
from tkinter.ttk import *
from tkinter import *

LARGE_FONT = ("Verdana", 12)
HEIGHT = 700
WIDTH = 800

class programStart(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side="top", fill="both", expand = True)
container.grid_rowconfigure(0, weight=1, minsize=WIDTH)
container.grid_columnconfigure(0, weight=1, minsize=HEIGHT) #0 minimum, weight is priority

self.frames = {}

for F in (StartPage, Register, LoginPage):
frame = F(container, self, bg="red")
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")

self.show_frame(StartPage)

def show_frame(self, cont):

frame = self.frames[cont]
frame.tkraise() #Raises to front

class StartPage(tk.Frame):
def __init__(self, parent, controller, bg=None, fg=None):
tk.Frame.__init__(self, parent, bg=bg=, fg=fg)
# Make sure that all of the tkinter widgets have `bg=bg=, fg=fg`

Basically you need to tell all of the widgets that you are creating that the background should be red. When creating your widgets you can pass in the bg parameter (background).



Related Topics



Leave a reply



Submit