直接从书中运行tkinter python代码(运行时错误)

时间:2014-12-24 19:38:08

标签: python debugging python-3.x tkinter runtime

我目前正在使用一本名为“编程Python”的书来学习python,这是一本非常好的书,它提供了许多代码示例,使其更易于理解。我在讨论tkinter的章节中,但是我发现当我把它们放在与书本完全相同的时候很多例子都不起作用(有些工作,这很奇怪) 。我知道这本书适用于python 3,而我的python版本是一样的,任何人都有任何想法?这是代码。

__author__ = 'MgenGlder'
from tkinter import *
from tkinter.messagebox import showinfo

class MyGui(Frame):
    def __init__(self, parent = None):
        Frame.__init__(self, parent)
        button = Button(self, test = 'press', command = self.reply)
        button.pack()
    def reply(self):
        showinfo(title = 'popup', message = 'Button pressed!')

if __name__ == '__main__':
    window = MyGui()
    window.pack()
    window.mainloop()

我得到的错误是:

C:\Python34\python.exe "C:/Users/MgenGlder/PycharmProjects/Programming Python/tkinter102.py"
Traceback (most recent call last):
  File "C:/Users/MgenGlder/PycharmProjects/Programming Python/tkinter102.py", line 14, in <module>
    window = MyGui()
  File "C:/Users/MgenGlder/PycharmProjects/Programming Python/tkinter102.py", line 8, in __init__
    button = Button(self, test = 'press', command = self.reply)
  File "C:\Python34\lib\tkinter\__init__.py", line 2192, in __init__
    Widget.__init__(self, master, 'button', cnf, kw)
  File "C:\Python34\lib\tkinter\__init__.py", line 2122, in __init__
    (widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: unknown option "-test"

1 个答案:

答案 0 :(得分:0)

错误消息告诉您究竟是什么问题(尽管有点神秘):

unknown option "-test"

问题在于这行代码:

button = Button(self, test = 'press', command = self.reply)

test = ...应为text = ...