python之Gui编程事件绑定 2014-4-8

place() 相对定位与绝对定位 相对定位 拖动会发生变化 绝对定位不会
from Tkinter import *
root = Tk()
# Absolute positioning
Button(root,text="Absolute Placement").place(x=20, y=10)
# Relative positioning
Button(root, text="Relative").place(relx=0.5, rely=0.2, relwidth=0.5,
width=10, anchor = NE)
root.mainloop()


from Tkinter import *
root = Tk()
Label(root, text=‘Click at different\n locations in the frame below‘).pack()
def mycallback(event):
print dir(event)
print "you clicked at", event.x, event.y
#event.x event.y 横坐标纵坐标
myframe = Frame(root, bg=‘khaki‘, width=130, height=80)
myframe.bind("<Button-1>", mycallback)
#Button-1是鼠标左击
#<KeyPress-B>按下鼠标B
#<Alt-Control-KeyPress- KP_Delete>
#Keyboard press of Alt + Ctrl + Delete
myframe.pack()
root.mainloop()

myframe.bind("<Button-1>", mycallback)绑定事件

注意点:An event type (required): Some common event types include Button,
ButtonRelease, KeyRelease, Keypress, FocusIn, FocusOut, Leave (mouse leaves
the widget ), and MouseWheel.

An event modifier (optional): Some common event modifiers include Alt, Any (used
like in <Any-KeyPress>), Control, Double (used like in <Double-Button-1> to
denote a double-click of the left mouse button ), Lock, and Shift

常用事件绑定
from Tkinter import *
root = Tk()
Label(root, text=‘Click at different\n locations in the frame below‘).pack()
def callback(event):
# print dir(event)
print "you clicked at", event.x, event.y
widget = Frame(root, bg=‘khaki‘, width=130, height=80)
widget.bind("<Button-1>",callback) #bind widget to left mouse click
widget.bind("<Button-2>", callback) # bind to right mouse click
widget.bind("<Return>", callback)# bind to Return(Enter) Key
widget.bind("<FocusIn>", callback) #bind to Focus in Event
widget.bind("<KeyPress-A>", callback)# bind to keypress A
widget.bind("<KeyPress-Caps_Lock>", callback)# bind to CapsLockkeysym
widget.bind("<KeyPress-F1>", callback)# bind widget to F1 keysym
widget.bind("<KeyPress-KP_5>", callback)# bind to keypad number 5
widget.bind(‘<Motion>‘, callback) # bind to motion over widget
widget.bind("<Any-KeyPress>", callback) # bind to any keypress
widget.pack()
root.mainloop()

python之Gui编程事件绑定 2014-4-8,布布扣,bubuko.com

python之Gui编程事件绑定 2014-4-8

上一篇:类A是公共的,应在名为A.java的文件中声明错误


下一篇:最好是在线程函数内发送消息给主窗口,主窗口响应消息控制进度条控件