[D]关于tkinter

forfrt 2012-05-04 08:37:19
最近开始学习tkinter,用的是an-introduction-to-tkinter,在实践书上代码时出的问题:

这是书上的自定义类:

# File: tkSimpleDialog.py
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.initial_focus = self.body(body)
body.pack(padx=5, pady=5)

self.buttonbox()

self.grab_set()

if not self.initial_focus:
self.initial_focus = self
self.protocol("WM_DELETE_WINDOW", self.cancel)
self.geometry("+%d+%d" % (parent.winfo_rootx()+50,
parent.winfo_rooty()+50))
self.initial_focus.focus_set()
self.wait_window(self)
#
# construction hooks

def body(self, master):
# create dialog body. return widget that should have
# initial focus. this method should be overridden

pass

def buttonbox(self):
# add standard button box. override if you don't want the
# standard buttons

box = Frame(self)

w = Button(box, text="OK", width=10, command=self.ok, default=ACTIVE)
w.pack(side=LEFT, padx=5, pady=5)
w = Button(box, text="Cancel", width=10, command=self.cancel)
w.pack(side=LEFT, padx=5, pady=5)

self.bind("<Enter>", self.ok)
self.bind("<Escape>", self.cancel)

box.focus_set()
box.pack()

#
# standard button semantics
def ok(self, event=None):
if not self.validate():
self.initial_focus.focus_set() # put focus back
return

self.withdraw()
self.update_idletasks()

self.apply()

self.cancel()

def cancel(self, event=None):
# put focus back to the parent window
self.parent.focus_set()
self.destroy()

#
# command hooks

def validate(self):

return 1 # override

def apply(self):

pass # override

这是上面类的继承类:
from Tkinter import *

import tkSimpleDialog
class MyDialog(tkSimpleDialog.Dialog):
def body(self, master):
Label(master, text="First:").grid(row=0)
Label(master, text="Second:").grid(row=1)

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

self.e1.grid(row=0, column=1)
self.e2.grid(row=1, column=1)
return self.e1 # initial focus

def apply(self):
first = (self.e1.get())
second = (self.e2.get())
print(first, second) # or something

root=Tk()
dialog=MyDialog(root)
root.mainloop()

然而每次运行时出现的dialog窗口在鼠标移上去没有进行任何操作就会消失,命令行上print:'','',查过了tkinter文档中涉及的每个方法,还是没找出原因,求教
-------------------------
Double行动:
原帖分数:40
帖子加分:40
...全文
75 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
angel_su 2012-05-04
  • 打赏
  • 举报
回复
你可能抄错了吧,self.bind("<Enter>", self.ok)这个就是绑定鼠标进入就apply退出,你没机会输入的。这个文件在Pythonxx\Lib\lib-tk里面就有的,它是self.bind("<Return>", self.ok)绑定回车才apply。所以把你提供的tkSimpleDialog.py删了吧...

37,741

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧