# -*- coding: utf-8 -*- """
20180912
學習 Frame 布局 bind 鍵盤事件 顯示圖片 使用: 1. 創建多個frame 對象,frame = Frame(...),沒有指定master,默認當前Tk對象 2 將各個frame 合適的布局在主面板上 3 往各個frame添加子控件 涉及: Frame,Text,Button,PhotoImage,Label 按鈕動作函數、 bind 事件 """ from Tkinter import * import time def msgsend(): '''send按鈕動作 發送消息,發送框內容消失,輸出框內容顯示 1、在<消息列表分區>的文本控件中實時添加時間; 2、獲取<發送消息分區>的文本內容,添加到列表分區的文本中; 3、將<發送消息分區>的文本內容清空。 ''' msg = '我'+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())+'\n' txt_msglist.insert(END, msg, 'green') # 表示 綠色字符插入到Text最后 txt_msglist.insert(END, txt_msgsend.get('0.0', END)) # 獲取發送消息,添加文本到消息列表 txt_msgsend.delete('0.0', END) # 清空發送消息 # cancle按鈕動作 取消消息發送 def cancel(): txt_msgsend.delete('0.0', END) # 清空發送消息 # 綁定up鍵 def msgsendEvent(event): if event.keysym == 'Up': # ↑按鈕 msgsend() tk = Tk() tk.title('聊天窗口') '''創建分區''' ## 在主面板上划分區域 # 注意Frame 沒有指定 master=tk,猜測沒有指定就用當前的 Tk() message_show_Frame = Frame(height = 200, width=300) # 創建<消息列表分區 > message_send_Frame = Frame(height = 200, width=300) # 創建<發送消息分區 > button_Frame = Frame(height=200, width=300) # 創建<按鈕分區> pic_right_Frame = Frame(height=600, width=100) # 創建<圖片分區> ## Frame在主控件上的布局 message_show_Frame.grid(row=0, column=0) message_send_Frame.grid(row=1, column=0) button_Frame.grid(row=2, column=0) pic_right_Frame.grid(row=0, column=1, rowspan=3) '''創建各個Frame中的控件''' ## 輸出Text txt_msglist = Text(message_show_Frame) txt_msglist.tag_config('green', foreground='blue') # 創建標簽,不懂 txt_msglist.grid() ## 輸入框Text txt_msgsend = Text(message_send_Frame) txt_msgsend.bind('<KeyPress-Up>', msgsendEvent) # 綁定‘UP’鍵與消息發送。 txt_msgsend.grid() ## 發送按鈕 button_send = Button(button_Frame, text='Send', command=msgsend) button_send.grid(row=0, column=0, sticky=W) # 在Frame f_floor上的布局設置 ## 取消按鈕 button_cancel = Button(button_Frame, text='Cancel', command=cancel) button_cancel.grid(row=0, column=1, sticky=W) ## 標簽顯示圖片 photo = PhotoImage(file=r'./no_time_for_that.gif') label = Label(pic_right_Frame, image=photo) label.image = photo label.grid() tk.mainloop()

來源:
https://blog.csdn.net/kun_dl/article/details/79500962
參考:
事件關聯
bind(sequence,func,add)——
bind_class(className,sequence,func,add)
bind_all(sequence,func,add)
事件參數:
sequence 所綁定的事件;
func 所綁定的事件處理函數;
add 可選參數,為空字符或‘+’;
className 所綁定的類;
鼠標鍵盤事件
<Button-1> 鼠標左鍵按下,2表示中鍵,3表示右鍵;
<ButtonPress-1> 同上;
<ButtonRelease-1> 鼠標左鍵釋放;
<B1-Motion> 按住鼠標左鍵移動;
<Double-Button-1> 雙擊左鍵;
<Enter> 鼠標指針進入某一組件區域;
<Leave> 鼠標指針離開某一組件區域;
<MouseWheel> 滾動滾輪;
<KeyPress-A> 按下A鍵,A可用其他鍵替代;
<Alt-KeyPress-A> 同時按下alt和A;alt可用ctrl和shift替代;
<Double-KeyPress-A> 快速按兩下A;
<Lock-KeyPress-A> 大寫狀態下按A;
窗口事件
Activate 當組件由不可用轉為可用時觸發;
Configure 當組件大小改變時觸發;
Deactivate 當組件由可用轉變為不可用時觸發;
Destroy 當組件被銷毀時觸發;
Expose 當組件從被遮擋狀態中暴露出來時觸發;
Unmap 當組件由顯示狀態變為隱藏狀態時觸發;
Map 當組件由隱藏狀態變為顯示狀態時觸發;
FocusIn 當組件獲得焦點時觸發;
FocusOut 當組件失去焦點時觸發;
Property 當窗體的屬性被刪除或改變時觸發;
Visibility 當組件變為可視狀態時觸發;
響應事件
event對象(def function(event)):
char 按鍵字符,僅對鍵盤事件有效;
keycode 按鍵名,僅對鍵盤事件有效;
keysym 按鍵編碼,僅對鍵盤事件有效;
num 鼠標按鍵,僅對鼠標事件有效;
type 所觸發的事件類型;
widget 引起事件的組件;
width,heigh 組件改變后的大小,僅Configure有效;
x,y 鼠標當前位置,相對於窗口;
x_root,y_root 鼠標當前位置,相對於整個屏幕

