Change color of progress bar Tkinter - python-3.x

I'm trying to change the color of a progress bar but the :
myProgressbar.configure(background="color")
It doesn't seems to work. I searched on the site, but didn't find an answer for the case of Tkinter.
Any idea ?
Thank you.

In python 2.7, you can do the same using theme. Here is the code:-
import Tkinter as tk
import ttk as ttk
root = tk.Tk()
frame = tk.Frame(root)
frame.grid()
s = ttk.Style()
s.theme_use('clam')
s.configure("red.Horizontal.TProgressbar", foreground='red', background='red')
ttk.Progressbar(frame, style="red.Horizontal.TProgressbar", orient="horizontal", length=600,mode="determinate", maximum=4, value=1).grid(row=1, column=1)
frame.pack()
tk.mainloop()
You can take a look over this link:-
How to change ttk.progressBar color in python

Related

What should I do to get the first example of ttk.style working properly?

I wanted to try the ttk.Style() settings, so I copied the example from the docs at Ttk Styling and ran it. I noticed that no matter what I set the "raised" parameter to, it looks the same. Here's the code I ran:
from tkinter import ttk
import tkinter
root = tkinter.Tk()
Running Python 3.10.8
ttk.Style().configure("TButton", padding=10, relief="raised",
background="#fff")
btn = ttk.Button(text="Sample")
btn.pack()
root.mainloop()
Running this, the button looks like this:
If I change "raised" to "flat" or "sunken", it looks the same. What should I do to see the button style change?
The button doesn't know you want it to use the style defined above. Try adding style=[style_name] to to the btn definition. Here is an example below.
from tkinter import ttk
import tkinter
root = tkinter.Tk()
style=ttk.Style()
style.configure("TButton", padding=10, relief="raised",
background="#fff")
btn = ttk.Button(text="Sample", style= "TButton")
btn.pack()
root.mainloop()
Hope it helps.

unable to apply a ttk theme to .toplevel()

I'm trying to rcreate a slasph screen which doubles as an about screen on a menu bar in tkinter and python 3.
so far I've read I should be using a Toplevel() method on the second splash_root but It justs send me the error that, name 'Toplevel' is not defined.
Have I missed something basic here or just misunderstood how Toplevel() works?
import tkinter as tk
import tkinter.ttk as ttk
from PIL import ImageTk, Image
from ttkthemes import ThemedTk
def splash_screen():
splash_root = Toplevel(root)
splash_root.geometry("500x400")
img_title = ImageTk.PhotoImage(Image.open("art/icons/random.png"))
lbl_img_title = ttk.Label(splash_root,image=img_title)
lbl_img_title.pack(side="top",pady=20)
splash_label_by = ttk.Label(splash_root, text="name")
splash_button = ttk.Button(splash_root, text="close", command=lambda: splash_root.destroy())
splash_label_by.pack(pady=20, padx=20)
splash_button.pack(ipadx=10, ipady=10)
splash_screen()
root = ThemedTk(themebg=True)
root.set_theme('black')
root.title("splash")
root.geometry("500x500")
root.mainloop()
The answer to your question:
#Toplevel(root) is not defined
tk.Toplevel(root) #will solve your Toplevel problem.
If you call
splash_screen()
before declaring root, root will be not defined and your code still wont work.

darken the whole tkinter window

I'm programming a game sa my graduation project in school. Once the manager closes the game, I want the other users to have a pop up that the game is ended and a button to click on in order to continue.
I want the whole window (with all the widgets) to get darker (not totally black but darker). Everything besides the pop up and the "continue" button
Enyone has a clue what I can do? I searched on the Internet and found nothing :/
Not able to use ImageGrab on linux, here is my solution:
import tkinter as tk
import numpy as np
import time
import pyscreenshot as ImageGrab
from PIL import Image, ImageTk
def grab(widget):
box = (widget.winfo_rootx(), widget.winfo_rooty(), widget.winfo_rootx()+widget.winfo_width(), widget.winfo_rooty()+widget.winfo_height())
grab = ImageGrab.grab(bbox=box)
return grab
def darken(widget):
widget.overlay = ImageTk.PhotoImage(Image.fromarray(np.asanyarray(grab(widget))//2))
cvs = tk.Canvas(widget, width=widget.winfo_width(), height=widget.winfo_height())
cvs.place(x=0,y=0)
cvs.create_image(0, 0, anchor='nw', image=widget.overlay)
return cvs
root = tk.Tk()
def apply_dark():
cvs = darken(root)
b = tk.Button(root, text='Ok')
def d():
cvs.destroy()
b.destroy()
b.config(command=d)
b.place(x=50, y=10)
tk.Label(root, text='Label1').grid(row=0, column=0)
tk.Label(root, text='Label2').grid(row=0, column=1)
tk.Button(root, text='Button1').grid(row=1, column=0)
tk.Button(root, text='Darken', command=apply_dark).grid(row=1, column=1)
root.mainloop()
Fiddle around with this code. Of course this is in fact just a quick bodge (non-reliable, inefficient), but still - works for me.
Hope that's helpful!

Why do two windows appear when I connect styles?

enter image description hereI have a code like this:
from tkinter import Tk
import tkinter.ttk as ttk
"""Styles"""
style = ttk.Style()
style.configure('OrangeButton.TButton', foreground='white', background='#ff9203')
style.map('OrangeButton.TButton',
foreground=[('pressed', 'white'), ('active', 'white')],
background=[('pressed', '!disabled', '#adadad'), ('active', '#de8e26')])
root = Tk()
button = ttk.Button(root, text="Ok", width=20, style='OrangeButton.TButton')
button.pack(padx=50, pady=50)
root.mainloop()
I'm new at this. I searched the Internet for a solution, but could not find it. Everywhere they write about widthdraw(), but this does not help. Two windows always appear and the customized style is not applied to the button. What am I doing wrong? How do I search Google for this problem? Tell me please. Thanks.
You just need to define ttk.Style() inside the root window.
from tkinter import Tk
import tkinter.ttk as ttk
root = Tk()
"""Styles"""
style = ttk.Style()
# add the these_use option and use the 'clam' theme
style.theme_use('clam')
style.configure('OrangeButton.TButton', foreground='white', background='#ff9203')
style.map('OrangeButton.TButton',
foreground=[('pressed', 'white'), ('active', 'white')],
background=[('pressed', '!disabled', '#adadad'), ('active', '#de8e26')])
button = ttk.Button(root, text="Ok", width=20, style='OrangeButton.TButton')
button.pack(padx=50, pady=50)
root.mainloop()
Hope this solves the problem.

how to set a theme for ttkthemes for python3 in windows10?

I'm trying to style my tkinter GUI with some theme from ttkthemes.
I have found this code :
from ttkthemes import ThemedStyle
import tkinter as tk
from tkinter import ttk
app = tk.Tk()
app.title('App')
style = ThemedStyle(app)
style.set_theme("black")
tktext = tk.Label(app, text=" tk Label")
tktext.pack()
tkbutton = tk.Button(app, text="tk Button")
tkbutton.pack()
text = ttk.Label(app, text=" ttk Label")
text.pack()
button = ttk.Button(app, text="ttk Button")
button.pack()
app.geometry('200x200')
app.mainloop()
in this topic :
Python - How do I add a theme from ttkthemes package to a guizero application?
but I have a problem with that and that's when I run the program the theme doesn't cover whole root windows and just the buttons or labels from ttk are taking the theme (using windows10).I have tried in some other codes and everytime the same problem.
What's Problem with that?
ttktheme
Themes only apply to widgets from the ttk module. For widgets not in ttk, such as the text widget, you have to configure them individually.

Resources