0

这是我的代码:

    print '1'
from Tkinter import *
print '2'
class myApp:

    print '3'
    def __init__(self,parent):
        print '4'

##        self.myparent = parent    line1
        print '11'

        self.myContainer1 = Frame(parent)
        print '12'
        self.myContainer1.pack()
        print '13'

        self.button1 = Button(self.myContainer1,text="OK",background="green")
        print '14'
        self.button1.pack(side=LEFT)
        print '15'
        self.button1.bind("<Button-1>",self.button1Click)
        print '16'

        self.button2 = Button(self.myContainer1,text="Cancel",background="cyan")
        print '17'
        self.button2.pack(side=RIGHT)
        print '18'
        self.button2.bind("<Button-1>",self.button2Click)
        print '19'


    def button1Click(self,event):

            print '5'

            if self.button1['background'] == 'green':
                print '20'
                self.button1['background'] ='tan'
                print '21'

            else:

                print '22'

                self.button1['background'] = 'yellow'

                print '23'

    def button2Click(self,event):

            print '6'

##            self.myparent.destroy()

            self.parent.destroy()

print '7'
root = Tk()
print '8'
myapp = myApp(root)
print '9'
root.mainloop()
print '10'

错误是:

    >>> ================================ RESTART ================================
>>> 
1
2
3
7
8
4
11
12
13
14
15
16
17
18
19
9
5
20
21
5
22
23
6
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python26\lib\lib-tk\Tkinter.py", line 1403, in __call__
    return self.func(*args)
  File "C:\Documents and Settings\he00044.HALEDGEWOOD\Desktop\TkinterExamples\buttonBind.py", line 56, in button2Click
    self.parent.destroy()
AttributeError: myApp instance has no attribute 'parent'
10
>>> 

这是我评论 line1 的时候

这可能是因为 myapp 没有找到父母。

但概念不清楚。

谁能详细解释一下这个概念......

4

4 回答 4

2

你为什么评论那两行提到self.myparent并创建一个新的提到一个神秘的,从未初始化的self.parent?!当然,这就是你的问题的开始——看起来很荒谬,故意破坏代码。

于 2009-07-03T06:23:14.433 回答
0

您的问题与 tkinter 无关,而是与面向对象设计有关。

myApp具有__init__方法(​​构造函数,在创建该类的对象时执行的方法),以及两个按钮单击方法。在 button2Click 方法中,您尝试读取属性self.parent(翻译为myapp.parent),但未定义此属性。

当您取消注释第 1 行时,您不会收到错误消息。这是因为您正在创建属性myapp.parent,并且您正在将 Tk 根小部件分配给该属性。这是必要的,因为您创建的所有小部件都必须接收它们的父小部件。

于 2009-07-03T07:25:15.590 回答
0

到目前为止,其他答案都很棒。
这也可能有所帮助:Fredrik Lundh 对 Tkinter 的介绍

在您的代码中添加了一些注释,连同其他答案,应该可以帮助您再次移动:

import Tkinter

class MyApp:
    def __init__(self, parent): # constructor
        self.parent = parent # the parent here is 'root'
        self.myContainer1 = Tkinter.Frame(self.parent) # create Frame w/ root as parent
        self.myContainer1.pack() # make Frame (myContainer1) visible
        self.button1 = Tkinter.Button(self.myContainer1, 
                        text="OK", background="green") # add button as child of Frame
        self.button1.pack(side=Tkinter.LEFT) # place button1 in Frame
        self.button1.bind("<Button-1>",self.button1Click) # bind left mouse button to button1Click method
        self.button2 = Tkinter.Button(self.myContainer1, text="Cancel", 
                        background="cyan")
        self.button2.pack(side=Tkinter.RIGHT)
        self.button2.bind("<Button-1>", self.button2Click)

    def button1Click(self, event):
        if self.button1['background'] == 'green':
            self.button1['background'] ='tan'
        else:
            self.button1['background'] = 'yellow'

    def button2Click(self, event):
        self.parent.destroy() # the parent here is 'root', so you're ending the event loop

root = Tkinter.Tk()     # create root widget (a window)
myapp = MyApp(root)     # create instance of MyApp with root as the parent
root.mainloop()         # create event loop (ends when window is closed)
于 2009-07-03T07:58:40.787 回答
0

将传入参数 parent 分配给 self.parent?

def __init__(self,parent):
    self.parent = parent
于 2009-07-03T06:19:03.623 回答