_tkinter.TclError:尝试将ttkcalendar添加到tkinter GUI时无法打包

时间:2016-04-05 10:17:32

标签: python tkinter ttk

我尝试在ttk calendar GUI中添加Tkinter。问题是它引发了_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

你知道问题出在哪里吗?

2 个答案:

答案 0 :(得分:2)

错误是您在班级 <?php session_start(); // this class is used to create connection with database class Database { private $db_host = ‘localhost’; private $db_user = ‘root’; private $db_pass = ‘root’; private $db_name = ‘test’; public function connect() { $db = new PDO('mysql:host=$db_host;dbname=$db_name;charset=utf8mb4', '$db_user', '$db_pass'); } } this class is used to insert the id in the table class table1 extends Database { public function insert_info() { $sql = "insert into info(id) values ('?')"; $sql->bind_param("s", $id); $sql->execute(); return true; } } $_SESSION['campid']='camp1001'; $db = new table1(); // it is used to object of class table1. $res=$db->insert_info(); ?> 中没有__init__方法。因此,只需将CalendarDialog方法重命名为body即可。现在,每次创建实例并初始化实例并定义__init__方法。

答案 1 :(得分:2)

我也遇到了将ttkCalendar放入对话框的问题。 我怀疑这篇文章的作者“借用”了与我建立日历相同的代码: https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&ved=2ahUKEwiWgYWKsJ3nAhVKl3IEHYrhCU8QFjABegQICBAB&url=https%3A%2F%2Fsvn.python.org%2Fprojects%2Fpython%2Ftrunk%2FDemo%2Ftkinter%2Fttk%2Fttkcalendar.py&usg=AOvVaw0ifTox4EI7CtBFWlRYD_m9

我发现使用此代码创建Calendar对象并将其放入对话框时有两个问题。 第一个导致回溯,如文章所示。解决方法是修改ttkcalendar.py文件以在创建日历时打包日历,而不是在使用pack()函数创建日历后打包日历。
这是差异:

102c102
<         self._calendar = ttk.Treeview(show='', selectmode='none', height=7)
---
>         self._calendar = ttk.Treeview(self, show='', selectmode='none', height=7)
109c109
<         self._calendar.pack(in_=self, expand=1, fill='both', side='bottom')
---
>         self._calendar.pack(expand=1, fill='both', side='bottom')

进行此更改后,日历将出现在对话框中。但是,您的问题尚未解决。尝试设置日历的最小大小时,会发生另一个异常:

Exception in Tkinter callback
Traceback (most recent call last):
  File "/home/richawil/Applications/anaconda3/envs/TLM/lib/python3.7/tkinter/__init__.py", line 1705, in __call__
    return self.func(*args)
  File "/home/richawil/Documents/Programming/Apps/TLM/TLM/ttkcalendar.py", line 134, in __minsize
    width, height = self._calendar.master.geometry().split('x')
AttributeError: 'Calendar' object has no attribute 'geometry'

除了注释对self .__ minsize的调用以外,我无法解决此问题。

63c62,63
<         self._calendar.bind('<Map>', self.__minsize)
---
>         # Commented out because _calendar object does not support geometry() function
>         #self._calendar.bind('<Map>', self.__minsize)