免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2624 | 回复: 1
打印 上一主题 下一主题

大家好,麻烦帮我看看一段关于Tk的python代码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-06 20:32 |只看该作者 |倒序浏览
#c://python25//env python
#-*-coding:UTF8-*-

from Tkinter import *
import os

class Dialog(Toplevel):

    def __init__(self,parent,title=None):

        Toplevel.__init__(self,parent)
        self.transient(parent)

        if title:
            self.title(title)                                        #这段代码有什么含义?

        self.parent=parent
        self.result=None

        body=Frame(self)

        self.inital_focus=self.body(body)
        body.pack(padx=5,pady=5)

        self.buttonbox()

        self.grab_set()                  # 这个方法是干什么用的?

        if not self.inital_focus:      
            self.inital_focus=self      ##这个有什么用??????????

        self.protocol("WM_DELETE_WINDOW",self.cancel)

        self.geometry("+%d+%d"%(parent.winfo_rootx()+5,parent.winfo_rooty()+50))    ##这个有什么用??????????

        self.inital_focus.focus_set()      # 这个方法是干什么用的?   

        self.wait_window(self)


        #结构钩子

        def body(self,master):
            #创建一个对话框,返回已有的框架
            #初始化焦点,
            Label(master,text="第一:").grid(row=0)
            Label(master,text="第二:").grid(row=1)

            self.e1=Entry(master)
            self.e2=Entry(master)

            self.e1.grid(row=0,column=1)
            self.e2.grid(row=1,colunm=1)

            return self.e1



        def buttonbox(self):
            #增加标准按钮
            #标准按钮

            box=Frame(self)
            w=Button(box,text="确定",width=10,command=self.ok,default=ACTIVE)
            w.pack(side=LEFT,padx=5,pady=5)
            w=Button(box,text="取消",width=10,command=self.cancel)
            w.pack(side=LEFT,padx=5,pady=5)

            self.bind("&It;Return>",self.ok)
            self.bind("&It;Escape>",self.cancel)

            box.pack()

        def ok(self,event=None):
            if not self.validate():
                self.inital_focus.focus_set()
                return
            self.withdraw()
            self.update_idletasks()

            self.apply()

            self.cancel()

        def cancel(self,event=None):
            #把 焦点还原父窗口
            self.parent.focus_set()
            self.destroy()

        #命令钩子
        def validate(self):          #这个是什么??
            return 1

        def apply(self):
            first=string.atoi(self.e1.get())
            second=string.atoi(self.e2.get())
            print first,second

论坛徽章:
0
2 [报告]
发表于 2008-12-06 20:49 |只看该作者

回复 #1 zengleo 的帖子

#c://python25//env python
#-*-coding:UTF8-*-

from Tkinter import *
import os

class Dialog(Toplevel):

    def __init__(self,parent,title=None):

        Toplevel.__init__(self,parent)
        self.transient(parent)

        if title:
            self.title(title)

        self.parent=parent
        self.result=None

        self.body=Frame(self)

        self.inital_focus=self.body(self.body)
        self.body.pack(padx=5,pady=5)

        self.buttonbox()

        self.grab_set()

        if not self.inital_focus:
            self.inital_focus=self

        self.protocol("WM_DELETE_WINDOW",self.cancel)

        self.geometry("+%d+%d"%(parent.winfo_rootx()+5,parent.winfo_rooty()+50))

        self.inital_focus.focus_set()

        self.wait_window(self)


        #结构钩子

        def body(self,master):
            #创建一个对话框,返回已有的框架
            #初始化焦点,
            Label(master,text="第一:").grid(row=0)
            Label(master,text="第二:").grid(row=1)

            self.e1=Entry(master)
            self.e2=Entry(master)

            self.e1.grid(row=0,column=1)
            self.e2.grid(row=1,colunm=1)

            return self.e1



        def buttonbox(self):
            #增加标准按钮
            #标准按钮

            box=Frame(self)
            w=Button(box,text="确定",width=10,command=self.ok,default=ACTIVE)
            w.pack(side=LEFT,padx=5,pady=5)
            w=Button(box,text="取消",width=10,command=self.cancel)
            w.pack(side=LEFT,padx=5,pady=5)

            self.bind("&It;Return>",self.ok)
            self.bind("&It;Escape>",self.cancel)

            box.pack()

        def ok(self,event=None):
            if not self.validate():
                self.inital_focus.focus_set()
                return
            self.withdraw()
            self.update_idletasks()

            self.apply()

            self.cancel()

        def cancel(self,event=None):
            #把 焦点还原父窗口
            self.parent.focus_set()
            self.destroy()

        #命令钩子
        def validate(self):
            return 1

        def apply(self):
            first=string.atoi(self.e1.get())
            second=string.atoi(self.e2.get())
            print first,second


root=Tk()
r=Dialog(root,title='测试')
r.mainloop()



有这个报错,
Traceback (most recent call last):
  File "C:\Documents and Settings\Administrator.ZENGLEO\桌面\a.py", line 101, in <module>
    r=Dialog(root,title='娴嬭瘯')
  File "C:\Documents and Settings\Administrator.ZENGLEO\桌面\a.py", line 22, in __init__
    self.inital_focus=self.body(self.body)
AttributeError: Frame instance has no __call__ method

能不能告诉我是为什么啊?
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP