python中事件如何處理

python中事件如何處理?針對(duì)這個(gè)問(wèn)題,這篇文章詳細(xì)介紹了相對(duì)應(yīng)的分析和解答,希望可以幫助更多想解決這個(gè)問(wèn)題的小伙伴找到更簡(jiǎn)單易行的方法。

創(chuàng)新互聯(lián)專注于企業(yè)營(yíng)銷型網(wǎng)站、網(wǎng)站重做改版、石峰網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、html5、商城網(wǎng)站建設(shè)、集團(tuán)公司官網(wǎng)建設(shè)、外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為石峰等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。

一、概述

一個(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)和鍵盤事件

python中事件如何處理三、event 對(duì)象常用屬性

python中事件如何處理四、鼠標(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ì)大家有一定的幫助,如果你還有很多疑惑沒(méi)有解開(kāi),可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識(shí)。

當(dāng)前題目:python中事件如何處理
URL地址:http://muchs.cn/article4/piheie.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、靜態(tài)網(wǎng)站網(wǎng)站營(yíng)銷、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站導(dǎo)航、網(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)

成都app開(kāi)發(fā)公司