Search code examples
pythontkinterttk

_tkinter.TclError: can't pack when trying to add ttkcalendar into tkinter GUI


I'm trying to add a ttk calendar into my Tkinter GUI. The problem is that it raises _tkinter.TclError: can't pack .34164128 inside .34161248.34161448.34161608

import Tkinter
import tkSimpleDialog

import ttkcalendar


class CalendarDialog(tkSimpleDialog.Dialog):
    """Dialog box that displays a calendar and returns the selected date"""

    def body(self, master):
        self.calendar = ttkcalendar.Calendar(master)
        self.calendar.pack()

    def apply(self):
        self.result = self.calendar.selection


# Demo code:
def main():
    root = Tkinter.Tk()
    root.wm_title("CalendarDialog Demo")

    def onclick():
        print 'click'

    cd = CalendarDialog(root)
    button = Tkinter.Button(root, text="Click me to see a calendar!", command=onclick)
    button.pack()
    root.update()

    root.mainloop()


if __name__ == "__main__":
    main()


TRACEBACK:
  File "C:/Users/Milano/PycharmProjects/MC/plots/ds.py", line 32, in <module>
    main()
  File "C:/Users/Milano/PycharmProjects/MC/plots/ds.py", line 23, in main
    cd = CalendarDialog(root)
  File "C:\Python27\lib\lib-tk\tkSimpleDialog.py", line 64, in __init__
    self.initial_focus = self.body(body)
  File "C:/Users/Milano/PycharmProjects/MC/plots/ds.py", line 9, in body
    self.calendar = ttkcalendar.Calendar(master)
  File "C:\Users\Milano\PycharmProjects\MC\plots\ttkcalendar.py", line 52, in __init__
    self.__place_widgets()      # pack/grid used widgets
  File "C:\Users\Milano\PycharmProjects\MC\plots\ttkcalendar.py", line 110, in __place_widgets
    self._calendar.pack(in_=self, expand=1, fill='both', side='bottom')
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1940, in pack_configure
    + self._options(cnf, kw))
_tkinter.TclError: can't pack .34164128 inside .34161248.34161448.34161608

Do you know where is the problem?


Solution

  • The fault is that you don't have an __init__ method in the class CalendarDialog. So just rename the body method to __init__. Now you have initialized the instance every time one is made and a pack() method is defined.