0

I learned how to implement ttk calendar from this post Python Tkinter ttk calendar All I want is including two ttk calendars button in my GUI interface. One for the 'arrival date' button and another for the 'return date' button. However, after I tried to include these two buttons in my GUI window, my GUI window becomes very laggy and slow to be loaded and it sometimes even freezes. Can someone give me some suggestion on what is the problem here?

Based on the link above, I tried to include on ttk calendar button first and everything works fine and my GUI window works smoothly. However, as long as I have two ttk calendar buttons being included, the whole GUI window is very laggy.

# from stackoverflow
# https://stackoverflow.com/questions/48298195/python-tkinter-ttk-calendar
class MyDateEntry(DateEntry):
    def __init__(self, master=None, **kw):
        DateEntry.__init__(self, master=master, **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')

...
...
...



# first button
ttk.Label(self.frame_entry_left_col, text='Arrival Date:').grid(row=6, column=0, padx=5, pady=(5, 0), sticky=tk.W)
self.fldArrivalDate = MyDateEntry(self.color, master=self.frame_entry_left_col, font=("Calibri", 8), background=self.color.secondary, width=17, selectmode='day')
self.fldArrivalDate.grid(row=7, column=0, padx=5, pady=(0, 6))

# second button
ttk.Label(self.frame_entry_left_col, text='Return Date:').grid(row=8, column=0, padx=5, pady=(5, 0), sticky=tk.W)
self.fldReturnDate = MyDateEntry(self.color, master=self.frame_entry_left_col, font=("Calibri", 8), width=17, selectmode='day')
self.fldReturnDate.grid(row=9, column=0, padx=5, pady=(0, 6))

I expect both buttons can work smoothly.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
bereshine
  • 1
  • 1
  • You are passing `self.color` as the first argument. Is `self.color` a frame? – Henry Yik Jun 30 '19 at 11:47
  • No, self.color is just the style of my GUI's buttons and labels. It is just a string indicating the color of this button. Thanks for the reply. – bereshine Jul 01 '19 at 16:43

1 Answers1

0

I don't reproduce your slowdown on Windows with Python 3.7. Your example is not really complete enough so what I actually tested is below.

A possible missing cause of the slowup is if you have conflicting pack and grid geometry management in a single frame. We can't see that from the code provided. In the below example you could pack the text widget and see if that causes a problem. In the version of Tkinter with Python 3.7 doing both packa nd grid in the same frame will raise an error but depending on the version of Tk you are using this might not occur on Python 2.7 and at one point this caused a lockup in Tk as the geometry managers fought against each other.

# https://stackoverflow.com/q/56811713/291641
#
# pip install tkcalendar

import sys
import tkinter as tk
import tkinter.ttk as ttk
from tkcalendar import DateEntry
from datetime import date

class MyDateEntry(DateEntry):
    def __init__(self, master=None, **kw):
        DateEntry.__init__(self, master=master, **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')

def main(args=None):
    root = tk.Tk()
    frame = ttk.Frame(root)

    row = 0
    for name in ['Arrival' , 'Departure']:
        label = ttk.Label(frame, text=name + ': ')
        cal = MyDateEntry(master=frame, width=17, selectmode='day')
        label.grid(row=row, column=0, sticky='news')
        cal.grid(row=row, column=1, sticky='news')
        row += 1

    text = tk.Text(frame)
    text.grid(row=row, columnspan=2, sticky='news')

    frame.grid_rowconfigure(row, weight=1)
    frame.grid_columnconfigure(1, weight=1)
    frame.grid(row=0, column=0, sticky='news')

    root.grid_rowconfigure(0, weight=1)
    root.grid_columnconfigure(0, weight=1)
    root.mainloop()
    return 0

if __name__ == '__main__':
    sys.exit(main(sys.argv[1:]))
patthoyts
  • 32,320
  • 3
  • 62
  • 93
  • Hey. Thanks for the response. Actually it may not be caused by confliction between different widget. The lagging starts even if I do not pack the second mydata entry widget. – bereshine Jul 14 '19 at 11:21