Skip to main content
 首页 » 编程设计

python之Tkinter 条目小部件出现问题

2025年05月04日18del

我正在关注 tkinter 的介绍,特别是第 29 页的对话框条目示例。http://www.ittc.ku.edu/~niehaus/classes/448-s04/448-standard/tkinter-intro.pdf

我收到以下错误:

d = MyDialog(root) 
TypeError: this constructor takes no arguments 

我删除了变量 d 的参数和 wait_window 的参数(参见下面的代码),程序将运行,但是没有输入字段。

这是代码

from Tkinter import * 
 
class MyDialog: 
 
    def init(self, parent): 
 
        top = Toplevel(parent) 
 
        Label(top, text="Value").pack() 
 
        self.e = Entry(top) 
        self.e.pack(padx=5) 
 
        b = Button(top, text="OK", command=self.ok) 
        b.pack(pady=5) 
 
    def ok(self): 
        print "value is", self.e.get() 
 
        self.top.destroy() 
 
root = Tk() 
Button(root, text="Hello!").pack() 
root.update() 
 
d = MyDialog(root) 
 
root.wait_window(d.top) 

请您参考如下方法:

改变

def init(self, parent): 

到

def __init__(self, parent): 

请参阅 object.__init__ 的文档.