Why do two windows appear when I connect styles? - python-3.x

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.

Related

Changing the background of ttk themed widgets in python

I am currently trying to learn ttk themed widgets. I wanted to change the background colour of my ttk button. I followed to ttk docs and written this:
from tkinter import *
from tkinter.ttk import *
root = Tk()
root.title("GUI App One")
root.geometry("800x500")
root.maxsize(800,500)
root.minsize(800,500)
Style().configure("C.TButton", padding=6, background="blue", relief="raised")
Style().configure("B.TButton", font=("Arial",30))
Style().configure("Elem.TFrame", background="red")
backframe = Frame(root, width=800, height=500, style="Elem.TFrame")
backframe.place(x=0, y=0)
print()
btn1 = Button(backframe, text="Click me", style="C.TButton")
btn1.place(x=20, y=50)
btn2 = Button(backframe, text="Click me too", style="B.TButton")
btn2.place(x=100, y=100)
mainloop()
in 'C.TButton' styling I tried to change the background colour of the 'btn1', but it only changes border colour to blue and not the background colour. How can I change the background colour?

Tkinter style not being applied

I'm simply trying to change to "clam" style but nothing changes. I have a more expansive UI written with other widgets, so I know that nothing changes when I try to change the style:
I'm working in Linux Mint 19.2, Python 3.6.8, and using Pycharm. I've verified that Pycharm is not the issue by running from terminal.
from tkinter import *
from tkinter.ttk import Style
class Window(Frame):
def __init__(self, master=None):
self.master = master
self.add_widgets()
def add_widgets(self, ):
self.mainframe = Frame(self.master, border=3)
self.mainframe.grid(columnspan=3, sticky='w', padx=5, pady=5)
Label(self.mainframe, text="Directory Selection", font='Helvetica 12 bold italic underline').grid(column=0, sticky="w")
self.pathframe = Frame(self.mainframe, relief=RIDGE, border=2)
self.pathframe.grid(rowspan=3, columnspan=3, sticky='w', padx=0, pady=10)
button_color="dark gray";
Button(self.pathframe, text="Run Folder Path", bg=button_color).grid(sticky="w")
Entry(self.pathframe, width=43).grid(row=0, column=1, sticky="w")
root = Tk()
style = Style()
style.theme_use("clam")
root.title('TestUI')
root.geometry("500x500")
app = Window(root)
root.mainloop()
I also tried:
style = Style(root)
style.theme_use("clam")
and still no change.
You're not using any ttk widgets. The normal tkinter widgets aren't affected by the ttk styles.
Typically this is done by importing ttk and then prefixing the widgets with ttk
from tkinter import ttk
...
self.mainframe = ttk.Frame(self.master, border=3)
...

Tkinter Toplevel window not appearing

Python 3.8, Win 10 is the os, Toplevel widget does not appear to be working with new window not appearing. Any guidance would be appreciated, thanks!
from tkinter import *
root = Tk()
def popup():
top = Toplevel(root)
my_label_top = Label(top, text="This is a Tkinter Popup")
top.mainloop()
my_button = Button(root, text="Popup, click here", command="popup")
my_button.grid(row=0, column=0)
root.mainloop()
Problem:
The only issue here is that the callback command shouldn't be a string.
Solution:
Remove the quotes around popup and the Toplevel window should appear.
Fixed Code:
from tkinter import *
root = Tk()
def popup():
top = Toplevel(root)
my_label_top = Label(top, text="This is a Tkinter Popup")
my_label_top.pack()
my_button = Button(root, text="Popup, click here", command=popup)
my_button.grid(row=0, column=0)
root.mainloop()
Tips:
Using top.mainloop() is not necessary.
You also forgot to pack() the Label(my_label_top)

How do I place tkinter buttons in a specific area?

I am making a program for finding booking contacts with ease. When I type in all of this information, the buttons stay to the top left of the window.
How can I get them in the middle of the screen? Better, how would I place them where-ever I want in general? (Don't worry about the webbrowser import for that is for later in the program.)
import webbrowser
from tkinter import *
from tkinter import ttk
root = Tk()
root.title('GUI Booking')
root.geometry('600x400')
root.resizable(width=False, height=False)
style = ttk.Style()
style.configure("TButton",
font="TkDefaultFont",
height=20,
width=20,
padding=10)
main_frame = Frame()
main_frame.grid(row=0, columnspan=4)
# Starting Window
button_location = ttk.Button(main_frame, text='Location').grid(row=1, column=3)
button_name = ttk.Button(main_frame, text='Name').grid(row=2, column=3)
button_email = ttk.Button(main_frame, text='Email').grid(row=3, column=3)
root.mainloop()
use the .place option instead of .grid:
...
Button = Button(something, text='Something', command=Something)
Button.place(x=value(e.g:2), y=value(e.g:2))
...
You could use row/column configure to center up the buttons: http://www.effbot.org/tkinterbook/grid.htm
I've added this under your root configuration. Ultimately, the position of widgets will depend on the widgets that you use and adjusting their position with the grid manager.
import webbrowser
from tkinter import *
from tkinter import ttk
root = Tk()
root.title('GUI Booking')
root.geometry('600x400')
root.resizable(width=False, height=False)
root.grid_rowconfigure(0, weight=1) # Added to center buttons
root.grid_columnconfigure(0, weight=1) # Added to center buttons
style = ttk.Style()
style.configure("TButton",
font="TkDefaultFont",
height=20,
width=20,
padding=10)
main_frame = Frame()
# Starting Window
button_location = ttk.Button(main_frame, text='Location').grid(row=1, column=1)
button_name = ttk.Button(main_frame, text='Name').grid(row=2, column=1)
button_email = ttk.Button(main_frame, text='Email').grid(row=3, column=1)
main_frame.grid(row=0, column=0)
root.mainloop()
if you want them to me in the middle of the screen, use .pack instead of .grid.
import webbrowser
from tkinter import *
from tkinter import ttk
root = Tk()
root.title('GUI Booking')
root.geometry('600x400')
root.resizable(width=False, height=False)
style = ttk.Style()
style.configure("TButton",
font="TkDefaultFont",
height=20,
width=20,
padding=10)
main_frame=Frame()
# Starting Window
button_location = ttk.Button(main_frame, text='Location').grid()
button_name = ttk.Button(main_frame, text='Name').grid()
button_email = ttk.Button(main_frame, text='Email').grid()
main_frame.pack()
root.mainloop()
warning: do not use .pack and .grid in the same window.

Change color of progress bar Tkinter

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

Resources