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:
Related
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()
Using dateentry from the tkcalendar python module, every time I select a date on the widget it shortens it to the m/d/yy format. I'd like it to expand to mm/dd/yyyy if possible, or at least for the year.
When using the set_date method, it will display whatever is inputted (string), but then reverts back to the shortened format once another date is clicked.
Is there any way to have it always display the full date? I can't seem to find a format parameter that would allow me to use %Y so that's why I ask.
This is possible without overriding _select since version 1.5.0, using the date_pattern argument (See documentation):
import tkinter as tk
from tkcalendar import DateEntry
window = tk.Tk()
DateEntry(window, locale='en_US', date_pattern='mm/dd/y').pack() # custom formatting
DateEntry(window, locale='en_US').pack() # default formatting
window.mainloop()
gives
If you just want to change how it appears upon selection, you can make a class that inherit DateEntry and override the _select method.
from tkcalendar import DateEntry
import tkinter as tk
root = tk.Tk()
class CustomDateEntry(DateEntry):
def _select(self, event=None):
date = self._calendar.selection_get()
if date is not None:
self._set_text(date.strftime('%m/%d/%Y'))
self.event_generate('<<DateEntrySelected>>')
self._top_cal.withdraw()
if 'readonly' not in self.state():
self.focus_set()
entry = CustomDateEntry(root)
entry._set_text(entry._date.strftime('%m/%d/%Y'))
entry.pack()
root.mainloop()
I would like to create a frame with
combobox
then two labels
and then another combobox.
Instead I get a frame with
two comboboxes
and then two labels
Tkinter widgets seem to be grouped by the widget type. Please advise how to place the widgets correctly. Thanks!
I am using Python 3.4 on Win 7 64 bit and Tcl/Tk version 8.6.
import tkinter as tk
from tkinter import ttk
class App(tk.Frame):
def __init__(self,master=None):
super().__init__(master)
self.grid()
self.combo1=ttk.Combobox(self)
self.combo1["values"]=["1","2"]
self.combo1.grid(row=1)
self.lbl1=ttk.Label(text="AAA")
self.lbl1.grid(row=2)
self.lbl3=ttk.Label(text="BBB")
self.lbl3.grid(row=3)
self.combo2=ttk.Combobox(self)
self.combo2["values"]=["3","4"]
self.combo2.grid(row=4)
root=tk.Tk()
x=App()
This happened because you didn't set the parent of your Labels to self (the frame), try changing your labels to this:
self.lbl1=ttk.Label(self, text="AAA")
...
self.lbl3=ttk.Label(self, text="BBB")
Previously they had used the default parent, which is root, so they appeared below your frame.
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..
Hi I am trying to use the ttk Combobox to create a dropdown with options .
While doing so i can configure the font size of the default value passed to it .
But when i click the arrow the font size of the other values remains the same .I am developing the app for touchscreen , so i need to provide proper size .
Heres the sample code , when i run the code the size of A is bigger , button the on clicking the arrow key i see the other values are of default size .
#! /usr/bin/python
from Tkinter import *
import ttk
class Application:
def __init__(self, parent):
self.parent = parent
self.combo()
def combo(self):
self.box_value = StringVar()
self.box = ttk.Combobox(self.parent, textvariable=self.box_value,font=("Helvetica",20))
self.box['values'] = ('A', 'B', 'C')
self.box.current(0)
self.box.grid(column=0, row=0)
if __name__ == '__main__':
root = Tk()
app = Application(root)
root.mainloop()
The thing is that the dropdown menu of the ttk Combobox is actually a simple Tkinter Listbox so it isn't affected by the ttk style. If it would be possible to get a reference to the Listbox from the Combobox, changing the font would be easy. However, I couldn't find a way to do so in Tkinter.
Edited as per patthoyts' very useful comment.
What you can do is change the font for all Listboxes that are part of a Combobox using
bigfont = tkFont.Font(family="Helvetica",size=20)
root.option_add("*TCombobox*Listbox*Font", bigfont)
That changes the font of all Listbox widgets that are part of a ttk Combobox and that are created after calling this.
This does affect all new Comboboxes, but I assume that's what you want. If you want the new font only for this Combobox, you could choose to create this Combobox as the last widget and call self.parent.option_add("*TCombobox*Listbox*Font", bigfont) right before creating this Combobox. Then only the Listbox under this Combobox will have the new font.
If you want all widgets to have the bigger font, you can use
root.option_add("*Font", bigfont)
or you can change the default font as described in this answer.
While working on the same issue as the OP, the problem of the arrow size mentioned in the comments of the accepted answer by Deepworks and fhdrsdg came up. Unfortunately I'm new and can't comment, hence I'm posting this as an answer. There is actually a way to set the arrow size via the Style "arrowsize" option.
style = ttk.Style()
style.configure('W.TCombobox',arrowsize = 60)
cBox = ttk.Combobox(self, style='W.TCombobox')
This allows you to increase the arrow size to match the font size of the rest of the widget.
I found the reference to the "arrowsize" option here:
Tcl8.6.10/Tk8.6.10 Documentation > Tk Commands > ttk_combobox