創(chuàng)新互聯(lián)www.cdcxhl.cn八線動(dòng)態(tài)BGP香港云服務(wù)器提供商,新人活動(dòng)買多久送多久,劃算不套路!
python中事件如何處理?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。
一、概述
一個(gè) GUI 應(yīng)用整個(gè)生命周期都處在一個(gè)消息循環(huán) (eventloop) 中。 它等待事件的發(fā)生, 并作出相應(yīng)的處理。Tkinter 提供了用以處理相關(guān)事件的機(jī)制. 處理函數(shù)可被綁定給各個(gè)控件的各種事件。
widget.bind(event, handler)
如果相關(guān)事件發(fā)生, handler 函數(shù)會(huì)被觸發(fā) , 事件對(duì)象event 會(huì)傳遞給 handler 函數(shù)。二
二、鼠標(biāo)和鍵盤事件
三、event 對(duì)象常用屬性
四、鼠標(biāo)事件和鍵盤事件用法測(cè)量
#coding=utf-8 from tkinter import * class application(Frame): def __init__(self,master): super().__init__(master) self.master=master self.pack() self.createWidget() def createWidget(self): self.canvas = Canvas(self,width=200,height=200,bg='green') self.canvas.pack() self.canvas.bind('<Button-1>',self.mouseTest) self.canvas.bind('<B1-Motion>',self.test_drag) self.canvas.bind('<KeyPress>',self.keyboard_test) self.canvas.bind('<KeyPress-a>',self.press_a_test) self.canvas.bind('KeyRelease-a',self.release_a_test) def mouseTest(self,event): print('鼠標(biāo)左鍵單機(jī)位置(相對(duì)于父容器):{0},{1}'.format(event.x,event.y)) print('鼠標(biāo)左鍵單擊位置(相對(duì)于屏幕):{0},{1}'.format(event.x_root,event.y_root)) print('事件綁定組件:{0}'.format(event.widget)) def test_drag(self,event): self.canvas.create_oval(event.x,event.y,event.x+1,event.y+1) def keyboard_test(self,event): print('鍵的keycode:{0},char:{1},keysym:{2}'.format(event.keycode,event.char,event.keysym)) def press_a_test(self,event): print('press a') def release_a_test(self): print('release a') if __name__ == '__main__': root = Tk() root.geometry('500x300') app=application(root) root.mainloop()
關(guān)于python中事件如何處理問(wèn)題的解答就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道了解更多相關(guān)知識(shí)。
新聞名稱:python中事件如何處理-創(chuàng)新互聯(lián)
文章源于:http://vcdvsql.cn/article38/hdcpp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、微信公眾號(hào)、網(wǎng)站改版、ChatGPT、App開發(fā)、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)