Tkinter style not being applied - python-3.x

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

Related

Why my ttk style won't apply to my Frame?

I've been fighting with ttk style for about 2 days right now, but nothing seems to work. I've tried a lot of different approaches, but I had no effect. Right now, I just want to see that the Frame that I'm trying to add to my App is working, could you please show me where is the problem?
import tkinter as tk
from tkinter import ttk
class App(tk.Tk):
def __init__(self):
super().__init__()
# Main window configuration
self.title("Work Tracker")
self.geometry("600x700")
self.resizable(0,0)
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
# Styles
style = ttk.Style()
style.configure('TFrame', background='blue')
# Logo frame
frame = ttk.Frame(self, width=200, height=150)
frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)
frame.grid_propagate(False)
frame.grid(row=0, column=0, sticky='nw')
label = ttk.Label(frame, text ="WORK TRACKER")
label.pack()
def main():
app = App()
app.mainloop()
main()

Is there a ttk equivalent of Scrolledtext widget Tkinter

I set the theme of my main window to an awtheme 'awdark'.All the widgets with ttk extension set its appearance according to the theme by itself all except for the scrolled text widget which appears white in colour(i.e, color of the field as well as the color and look of the scrollbar)probably because its not a part of ttk.My scrolledtext widget is contained in a ttk.Frame widget by the way.Is there any workaround this?
Is there a ttk equivalent of Scrolledtext widget Tkinter
No, there is not. The ttk widgets don't have a text widget.
The scrolledtext widget is just a text widget and scrollbars, there's not much more to it. You can create your own which uses ttk scrollbars with just a few lines of code.
Here's a solution that doesn't use classes. One that is class-based is just a couple extra lines of code.
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
frame = ttk.Frame(root)
frame.pack(fill="both", expand=True)
text = tk.Text(frame, wrap="none")
vsb = ttk.Scrollbar(frame, command=text.yview, orient="vertical")
hsb = ttk.Scrollbar(frame, command=text.xview, orient="horizontal")
text.configure(yscrollcommand=vsb.set, xscrollcommand=hsb.set)
frame.grid_rowconfigure(0, weight=1)
frame.grid_columnconfigure(0, weight=1)
vsb.grid(row=0, column=1, sticky="ns")
hsb.grid(row=1, column=0, sticky="ew")
text.grid(row=0, column=0, sticky="nsew")
root.mainloop()

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 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.

How can i create a Modal Dialogue Box by messagebox.showerror

How can I create a Modal Dialogue Box by messagebox.showerror?
messagebox.showerror("Error", "No downloader.exe found")
When I create a messagebox, I found I can move the root windows.
and i need to create a Modal Dialogue Box like filedialog.askopenfilename.
filedialog.askopenfilename(initialdir = self.get_path()+ '/bin', filetypes=[("BIN Files", ".bin")])
here's the codes:
import tkinter
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
import os
class Application(Frame):
def createWidgets(self, main_frame):
#self.llabel = Label(main_frame, text="Ready", width=20, bg="turquoise", font = ftLabel)
#self.llabel.grid(row=0, column=0, sticky=W+E) #columnspan=2
self.frame1 = Frame(main_frame)
self.frame1.grid(row=0, column=0, columnspan=2, sticky=W+E+N+S)
self.addr = StringVar()
self.addrtext = Entry(self.frame1, width=20, textvariable = self.addr)
self.addrtext.grid(row=0, column=0, sticky=W+E+N+S)
self.addr.set("0x0")
self.bfile = Button(self.frame1, text='BIN File', width=20)
self.bfile.grid(row=0, column=1, sticky=W+E+N+S)
messagebox.showerror("Error", "No downloader.exe found")
def __init__(self, master=None):
Frame.__init__(self, master)
self.pack(fill=BOTH, expand=1)
main_frame = Frame(master)
main_frame.pack(fill="y", expand=1)
self.createWidgets(main_frame)
self.dl_thread = 0
if __name__=="__main__":
root = Tk()
#lock the root size
root.resizable(False,False)
app = Application(master=root)
app.mainloop()
I tried your code and messagebox.showerror is modal to me.
Maybe there is something else in your code (threads?) or maybe it's
dependent on your environment.
For reference, my entire code:
from tkinter import *
from tkinter import messagebox
root = Tk()
def do(): messagebox.showerror("Error", "No downloader.exe found")
b = Button(root, text='Dialog', command=do)
b.pack()
root.mainloop()
If that doesn't work you might want to take a look at: Tkinter messagebox not behaving like a modal dialog

Resources