I'm working on my GUI with python ttk; and I have this problem where the "check" on my ttk checkbuttons won't show with my style, so there's no way to know if it's activated or not. I've tried changing different style options, but I can't find any such option for the "default" theme.
I do not want to change my theme as that would ruin my other styles.
How can I change the makeup and layout of a ttk checkbutton?
from tkinter import ttk
tk = Tk()
style = ttk.Style()
style.theme_use('default')
ttk.Checkbutton(tk, text='hello there').grid()
Related
I am trying to change the focus color of a ttk.Entry widget in python (blue border):
I know that we can change the focus color of the notebook tabs with style.configure('Tab', focuscolor='red'), so I wonder how to do it with an entry widget?
Here is my code:
import tkinter
from tkinter import ttk
root = tkinter.Tk()
style = ttk.Style()
style.theme_use('clam')
style.configure('TEntry', focuscolor='red') # don't work
ttk.Entry(root).grid(padx=10, pady=10)
root.mainloop()
As suggested by #Thingamabobs and #acw1668 It's possible to change the focus color of a ttk.Entry by mapping the focus state in its style property. Here is the working code:
import tkinter
from tkinter import ttk
root = tkinter.Tk()
style = ttk.Style()
style.theme_use('clam')
style.map('TEntry', lightcolor=[('focus', 'white')])
ttk.Entry(root).grid(padx=10, pady=10)
root.mainloop()
My final goal was to hide the focus border so I changed its color to white (the background color), and now the result looks like this: (The black border is just the original border of the entry)
In my real code I have a main window where the user can choose to open other kind of windows. In the main one, I defined the ttk style using the ttk.style() statement. It works, but if I define the same style in the other classes dedicated for the other windows, the ttk.style() doesn't work anymore. Why? Below is an example:
from tkinter import *
from tkinter import ttk
class MainWindow:
def __init__(self):
self.parent=Tk()
self.parent.geometry("400x400")
self.parent.title(self.main_name)
self.parent.configure(background="#f0f0f0")
style=ttk.Style()
style.configure("TButton", background="red", padding=0)
MyButton=ttk.Button(self.parent, text="open a new window", command=self.Open)
MyButton.pack()
self.parent.mainloop()
def Open(self):
obj=NewWindow()
class NewWindow():
def __init__(self):
self.parent=Tk()
self.parent.geometry("400x400")
self.parent.configure(background="#f0f0f0")
style=ttk.Style()
style.configure("TButton", background="red", padding=0)
MyButton=ttk.Button(self.parent, text="This button has not a custom style.. why?")
MyButton.pack()
if __name__=="__main__":
app=MainWindow()
Why the window from the NewWindow class doesn't use the custom ttk style like the other one from the MainWindow class?
Then I want to write just one time the ttk instructions, because in my real code, all classes use the same style. What is the best way to do it?
Below is a screenshot about my example:
Every instance of Tk is a separate environment, and cannot share data with other instances of Tk. If you want multiple windows to be able to share information with the first window, you must create instances of Toplevel rather than Tk.
The reason your second window doesn't accept the new styling is that the Style object you created belongs to the original root window. If you want it to affect the new root window you must explicitly tell it so by specifying the master attribute.
style=ttk.Style(master=self.parent)
When I disable a button the color change automatically to black.
This is the code :
from tkinter import *
from tkinter import ttk
root=Tk()
style=ttk.Style()
style.configure('TButton', foreground='red')
bu1=ttk.Button(root, text="Hello world")
bu1.grid(row=0, column=0)
bu2=ttk.Button(root, text="Hello world2")
bu2.grid(row=1, column=0)
bu1.state(['disabled'])
bu2.state(['disabled'])
root.mainloop()
Any help?
Since you are using a ttk button, you can map certain attributes to different button states with the map method of the style object.
For example, to change the colors when the button state is "disabled", you can set the color like this:
style.map(
"TButton",
foreground=[("disabled", "black")]
)
For more information see 50.2. ttk style maps: dynamic appearance changes on the New Mexico Tech tkinter documentation, and also Styles and Themes on tkdocs.com
I am trying to create a drop down calendar for a date entry.
Below is a portion of my code:
The drop down portion of it dosen't work and I can't seem to find the syntax for DateEntry() of ttk calendar anywhere to include the calendar widget option!
#creating the frame
from tkinter import *
from tkcalendar import *
root = Tk()
f1=Frame(root,width=1500,height=100,relief=SUNKEN,bd=4,bg='light steel blue')
f1.pack(side=TOP)
f2=Frame(root,width=1500,height=550,relief=SUNKEN,bd=4,bg='white')
f2.pack()
f3=Frame(root,width=1600,height=100,relief=SUNKEN,bd=4,bg='white')
f3.pack(side=BOTTOM)
#Creating the date column
l4=Label(f2,text='DATE',font=('tahoma',20,'bold'),fg='black',anchor='w')
l4.grid(row=0,column=3)
cal=DateEntry(f2,dateformat=3,width=12, background='darkblue',
foreground='white', borderwidth=4,Calendar =2018)
cal.grid(row=1,column=3,sticky='nsew')
I want it to look like this:
UPDATE: I have fixed the issue and published a new version of tkcalendar.
EDIT: the problem is that in Windows, the drop-down does not open when the downarrow button is clicked. It seems that it comes from the default ttk theme for Windows because it works with other themes. So the workaround is to switch theme and use 'clam' for instance ('alt' should work as well). Meanwhile, I will look into it and see if I can fix the DateEntry for the other themes and release a new version (https://github.com/j4321/tkcalendar/issues/3).
I am not sure what you want to achieve exactly with the DateEntry, but if your goal is to make it look like the one in the picture, it can be done the following way:
import tkinter as tk
from tkinter import ttk
from tkcalendar import DateEntry
from datetime import date
root = tk.Tk()
# change ttk theme to 'clam' to fix issue with downarrow button
style = ttk.Style(root)
style.theme_use('clam')
class MyDateEntry(DateEntry):
def __init__(self, master=None, **kw):
DateEntry.__init__(self, master=None, **kw)
# add black border around drop-down calendar
self._top_cal.configure(bg='black', bd=1)
# add label displaying today's date below
tk.Label(self._top_cal, bg='gray90', anchor='w',
text='Today: %s' % date.today().strftime('%x')).pack(fill='x')
# create the entry and configure the calendar colors
de = MyDateEntry(root, year=2016, month=9, day=6,
selectbackground='gray80',
selectforeground='black',
normalbackground='white',
normalforeground='black',
background='gray90',
foreground='black',
bordercolor='gray90',
othermonthforeground='gray50',
othermonthbackground='white',
othermonthweforeground='gray50',
othermonthwebackground='white',
weekendbackground='white',
weekendforeground='black',
headersbackground='white',
headersforeground='gray70')
de.pack()
root.mainloop()
I created a class inheriting from DateEntry to add the label with today's date below the calendar and to create a black border around the drop-down (self._top_cal is the Toplevel containing the calendar).
Then, I created an instance of MyDateEntry and with all calendar options needed to make it look like the picture. In addition, I used the year, month, day options to define the initial date inside the entry.
Here is the result:
The widget shows tick using tickinterval when following code shown below,
from Tkinter import *
slider_1 = Scale(mGui,orient=HORIZONTAL,length = 100,from_=0,to=9, tickinterval =1).pack()
However it throws error with the following code
from Tkinter import *
from ttk import *
slider_1 = Scale(mGui,orient=HORIZONTAL,length = 100,from_=0,to=9, tickinterval =1).pack()
Error:
_tkinter.TclError: unknown option "-tickinterval"
Why is it so? Is it a bug or problem with the installation. For information i am using Python 2.7.10
This is because the ttk module contains also a Scale widget, and you are actually using the Scale widget from ttk and not from Tkinter. Widgets in the ttk module are customised and styled differently from Tkinter widgets.
Check the following documentation on ttk for more information regarding its widgets:
ttk — Tk themed widgets
To solve your problem, you could remove your second global import and simply do:
import ttk
Then, every time you want to use a widget from ttk, you can simply prefix it with ttk..