最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

詳解python使用canvas實(shí)現(xiàn)移動(dòng)并綁定鍵盤

 更新時(shí)間:2021年12月20日 16:04:24   作者:Sandy_Star  
這篇文章主要為大家介紹了python使用canvas實(shí)現(xiàn)移動(dòng)并綁定鍵盤,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助

一、任務(wù)

用多個(gè)按鈕或者按鈕+文本框?qū)崿F(xiàn)不同物體(橢圓,長(zhǎng)方形,扇形)的各個(gè)方向的運(yùn)動(dòng)(上下左右)

效果圖:

image-20211216140806602

二、實(shí)現(xiàn)

(1)導(dǎo)庫并創(chuàng)建畫布

import tkinter as tk
window = tk.Tk()
window.title('my window')
##窗口尺寸
window.geometry('300x350')
#新建畫布
canvas=tk.Canvas(window,bg='blue',height=150,width=300)

(2)畫圖形

#畫線
x0,y0,x1,y1=50,50,80,80
line=canvas.create_line(x0,y0,x1,y1)
#畫?
oval=canvas.create_oval(x0,y0,x1,y1,fill='red')
#畫一個(gè)扇形
arc = canvas.create_arc(x0+30, y0+30, x1+30, y1+30, start=0, extent=90)
#畫一個(gè)矩形
rect = canvas.create_rectangle(100, 30, 100+20, 30+20)   
canvas.pack()

(3)創(chuàng)建按鈕和文本框

#創(chuàng)建文本框
entry = tk.Entry(window, show=None)
entry.pack()
#創(chuàng)建文本
label = tk.Label(window , text='圓:oval;線:line;扇形:arc;矩形 :rect')
label.pack()  #打包
#創(chuàng)建一個(gè)Button
b=tk.Button(window,text='下',command=moveit)
b.place(x=120,y=280)
up=tk.Button(window,text='上',command=moveup)
up.place(x=120,y=220)
left=tk.Button(window,text='左',command=moveleft)
left.place(x=80,y=250)
right=tk.Button(window,text='右',command=moveright)
right.place(x=160,y=250)

(4)功能實(shí)現(xiàn)

#向下移動(dòng)
def moveit():
    obj = entry.get()  #獲取輸入框的參數(shù),移動(dòng)那個(gè)圖形
    if obj =='rect':
        canvas.move(rect,0,2)  #第一個(gè)參數(shù)是圖形
    elif obj == 'arc':
        canvas.move(arc, 0, 2)  # 第一個(gè)參數(shù)是圖形
    elif obj =='oval':
        canvas.move(oval, 0, 2)  # 第一個(gè)參數(shù)是圖形
    elif obj == 'line':
        canvas.move(line, 0, 2)
#向上移動(dòng)
def moveup():
    obj = entry.get()  # 獲取輸入框的參數(shù),移動(dòng)那個(gè)圖形
    if obj =='rect':
        canvas.move(rect,0,-2)  #第一個(gè)參數(shù)是圖形
    elif obj == 'arc':
        canvas.move(arc,0,-2)  # 第一個(gè)參數(shù)是圖形
    elif obj =='oval':
        canvas.move(oval,0,-2)  # 第一個(gè)參數(shù)是圖形
    elif obj == 'line':
        canvas.move(line,0,-2)
#向左移動(dòng)
def moveleft():
    obj = entry.get()  # 獲取輸入框的參數(shù),移動(dòng)那個(gè)圖形
    if obj =='rect':
        canvas.move(rect, -2 ,0)  #第一個(gè)參數(shù)是圖形
    elif obj == 'arc':
        canvas.move(arc, -2 ,0)  # 第一個(gè)參數(shù)是圖形
    elif obj =='oval':
        canvas.move(oval, -2 ,0)  # 第一個(gè)參數(shù)是圖形
    elif obj == 'line':
        canvas.move(line, -2, 0)
#向右移動(dòng)
def moveright():
    obj = entry.get()  # 獲取輸入框的參數(shù),移動(dòng)那個(gè)圖形
    if obj =='rect':
        canvas.move(rect,2,0)  #第一個(gè)參數(shù)是圖形
    elif obj == 'arc':
        canvas.move(arc, 2,0)  # 第一個(gè)參數(shù)是圖形
    elif obj =='oval':
        canvas.move(oval, 2,0)  # 第一個(gè)參數(shù)是圖形
    elif obj == 'line':
        canvas.move(line , 2, 0)

三、完整代碼

import tkinter as tk
window = tk.Tk()
window.title('my window')
##窗口尺寸
window.geometry('300x350')
#新建畫布
canvas=tk.Canvas(window,bg='blue',height=150,width=300)
#畫線
x0,y0,x1,y1=50,50,80,80
line=canvas.create_line(x0,y0,x1,y1)
#畫?
oval=canvas.create_oval(x0,y0,x1,y1,fill='red')
#畫一個(gè)扇形
arc = canvas.create_arc(x0+30, y0+30, x1+30, y1+30, start=0, extent=90)
#畫一個(gè)矩形
rect = canvas.create_rectangle(100, 30, 100+20, 30+20)   
canvas.pack()
#創(chuàng)建文本框
entry = tk.Entry(window, show=None)
entry.pack()
#創(chuàng)建文本
label = tk.Label(window , text='圓:oval;線:line;扇形:arc;矩形 :rect')
label.pack()  #打包
#向下移動(dòng)
def moveit():
    obj = entry.get()  #獲取輸入框的參數(shù),移動(dòng)那個(gè)圖形
    if obj =='rect':
        canvas.move(rect,0,2)  #第一個(gè)參數(shù)是圖形
    elif obj == 'arc':
        canvas.move(arc, 0, 2)  # 第一個(gè)參數(shù)是圖形
    elif obj =='oval':
        canvas.move(oval, 0, 2)  # 第一個(gè)參數(shù)是圖形
    elif obj == 'line':
        canvas.move(line, 0, 2)
#向上移動(dòng)
def moveup():
    obj = entry.get()  # 獲取輸入框的參數(shù),移動(dòng)那個(gè)圖形
    if obj =='rect':
        canvas.move(rect,0,-2)  #第一個(gè)參數(shù)是圖形
    elif obj == 'arc':
        canvas.move(arc,0,-2)  # 第一個(gè)參數(shù)是圖形
    elif obj =='oval':
        canvas.move(oval,0,-2)  # 第一個(gè)參數(shù)是圖形
    elif obj == 'line':
        canvas.move(line,0,-2)
#向左移動(dòng)
def moveleft():
    obj = entry.get()  # 獲取輸入框的參數(shù),移動(dòng)那個(gè)圖形
    if obj =='rect':
        canvas.move(rect, -2 ,0)  #第一個(gè)參數(shù)是圖形
    elif obj == 'arc':
        canvas.move(arc, -2 ,0)  # 第一個(gè)參數(shù)是圖形
    elif obj =='oval':
        canvas.move(oval, -2 ,0)  # 第一個(gè)參數(shù)是圖形
    elif obj == 'line':
        canvas.move(line, -2, 0)
#向右移動(dòng)
def moveright():
    obj = entry.get()  # 獲取輸入框的參數(shù),移動(dòng)那個(gè)圖形
    if obj =='rect':
        canvas.move(rect,2,0)  #第一個(gè)參數(shù)是圖形
    elif obj == 'arc':
        canvas.move(arc, 2,0)  # 第一個(gè)參數(shù)是圖形
    elif obj =='oval':
        canvas.move(oval, 2,0)  # 第一個(gè)參數(shù)是圖形
    elif obj == 'line':
        canvas.move(line , 2, 0)
#創(chuàng)建一個(gè)Button
b=tk.Button(window,text='下',command=moveit)
b.place(x=120,y=280)
up=tk.Button(window,text='上',command=moveup)
up.place(x=120,y=220)
left=tk.Button(window,text='左',command=moveleft)
left.place(x=80,y=250)
right=tk.Button(window,text='右',command=moveright)
right.place(x=160,y=250)
##顯示出來
window.mainloop()

四、升級(jí)—綁定鍵盤事件

增加鍵盤綁定事件:

# 實(shí)現(xiàn)鍵盤綁定
window.bind("<KeyPress-Down>", moveit)  #第二個(gè)參數(shù)傳一個(gè)回調(diào)函數(shù)
window.bind("<KeyPress-Left>", moveleft)
window.bind("<KeyPress-Right>", moveright)
window.bind("<KeyPress-Up>", moveup)

完整代碼:

import tkinter as tk
window = tk.Tk()
window.title('my window')
##窗口尺寸
window.geometry('300x350')
#新建畫布
canvas=tk.Canvas(window,bg='blue',height=150,width=300)
#畫線
x0,y0,x1,y1=50,50,80,80
line=canvas.create_line(x0,y0,x1,y1)
#畫?
oval=canvas.create_oval(x0,y0,x1,y1,fill='red')
#畫一個(gè)扇形
arc = canvas.create_arc(x0+30, y0+30, x1+30, y1+30, start=0, extent=90)
#畫一個(gè)矩形
rect = canvas.create_rectangle(100, 30, 100+20, 30+20)   
canvas.pack()
#創(chuàng)建文本框
entry = tk.Entry(window, show=None)
entry.pack()
#創(chuàng)建文本
label = tk.Label(window , text='圓:oval;線:line;扇形:arc;矩形 :rect')
label.pack()  #打包
#向下移動(dòng)
def moveit(event):
    obj = entry.get()  #獲取輸入框的參數(shù),移動(dòng)那個(gè)圖形
    if obj =='rect':
        canvas.move(rect,0,2)  #第一個(gè)參數(shù)是圖形
    elif obj == 'arc':
        canvas.move(arc, 0, 2)  # 第一個(gè)參數(shù)是圖形
    elif obj =='oval':
        canvas.move(oval, 0, 2)  # 第一個(gè)參數(shù)是圖形
    elif obj == 'line':
        canvas.move(line, 0, 2)
#向上移動(dòng)
def moveup(event):
    obj = entry.get()  # 獲取輸入框的參數(shù),移動(dòng)那個(gè)圖形
    if obj =='rect':
        canvas.move(rect,0,-2)  #第一個(gè)參數(shù)是圖形
    elif obj == 'arc':
        canvas.move(arc,0,-2)  # 第一個(gè)參數(shù)是圖形
    elif obj =='oval':
        canvas.move(oval,0,-2)  # 第一個(gè)參數(shù)是圖形
    elif obj == 'line':
        canvas.move(line,0,-2)
#向左移動(dòng)
def moveleft(event):
    obj = entry.get()  # 獲取輸入框的參數(shù),移動(dòng)那個(gè)圖形
    if obj =='rect':
        canvas.move(rect, -2 ,0)  #第一個(gè)參數(shù)是圖形
    elif obj == 'arc':
        canvas.move(arc, -2 ,0)  # 第一個(gè)參數(shù)是圖形
    elif obj =='oval':
        canvas.move(oval, -2 ,0)  # 第一個(gè)參數(shù)是圖形
    elif obj == 'line':
        canvas.move(line, -2, 0)
#向右移動(dòng)
def moveright(event):
    obj = entry.get()  # 獲取輸入框的參數(shù),移動(dòng)那個(gè)圖形
    if obj =='rect':
        canvas.move(rect,2,0)  #第一個(gè)參數(shù)是圖形
    elif obj == 'arc':
        canvas.move(arc, 2,0)  # 第一個(gè)參數(shù)是圖形
    elif obj =='oval':
        canvas.move(oval, 2,0)  # 第一個(gè)參數(shù)是圖形
    elif obj == 'line':
        canvas.move(line , 2, 0)
#創(chuàng)建一個(gè)Button
b=tk.Button(window,text='下',command=moveit)
b.place(x=120,y=280)
up=tk.Button(window,text='上',command=moveup)
up.place(x=120,y=220)
left=tk.Button(window,text='左',command=moveleft)
left.place(x=80,y=250)
right=tk.Button(window,text='右',command=moveright)
right.place(x=160,y=250)
# 實(shí)現(xiàn)鍵盤綁定
window.bind("<KeyPress-Down>", moveit)  #第二個(gè)參數(shù)傳一個(gè)回調(diào)函數(shù)
window.bind("<KeyPress-Left>", moveleft)
window.bind("<KeyPress-Right>", moveright)
window.bind("<KeyPress-Up>", moveup)
##顯示出來
window.mainloop()

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • python flask框架快速入門

    python flask框架快速入門

    Flask 本身相當(dāng)于一個(gè)內(nèi)核,其他幾乎所有的功能都要用到擴(kuò)展,都需要用第三方的擴(kuò)展來實(shí)現(xiàn),本文給大家分享如何快速入門python flask框架,感興趣的朋友一起看看吧
    2021-05-05
  • Python中元組的概念及應(yīng)用小結(jié)

    Python中元組的概念及應(yīng)用小結(jié)

    Python中的元組和列表很相似,元組也是Python語言提供的內(nèi)置數(shù)據(jù)結(jié)構(gòu)之一,可以在代碼中直接使用,這篇文章主要介紹了Python中元組的概念以及應(yīng)用,需要的朋友可以參考下
    2023-01-01
  • 帶你了解Python語言的神奇世界

    帶你了解Python語言的神奇世界

    大家好,本篇文章主要講的是帶你了解Python語言的神奇世界,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽
    2021-12-12
  • python遞歸函數(shù)求n的階乘,優(yōu)缺點(diǎn)及遞歸次數(shù)設(shè)置方式

    python遞歸函數(shù)求n的階乘,優(yōu)缺點(diǎn)及遞歸次數(shù)設(shè)置方式

    這篇文章主要介紹了python遞歸函數(shù)求n的階乘,優(yōu)缺點(diǎn)及遞歸次數(shù)設(shè)置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • 使用Python對(duì)Csv文件操作實(shí)例代碼

    使用Python對(duì)Csv文件操作實(shí)例代碼

    這篇文章主要介紹了使用Python對(duì)Csv文件操作實(shí)例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2017-05-05
  • tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this T

    tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU

    這篇文章主要介紹了tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this T的相關(guān)知識(shí),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-06-06
  • Python約瑟夫生者死者小游戲?qū)嵗v解

    Python約瑟夫生者死者小游戲?qū)嵗v解

    在本篇文章里小編給大家分享的是一篇關(guān)于Python約瑟夫生者死者小游戲?qū)嵗v解內(nèi)容,有興趣的朋友們可以測(cè)試學(xué)習(xí)下。
    2021-01-01
  • python抓取并保存html頁面時(shí)亂碼問題的解決方法

    python抓取并保存html頁面時(shí)亂碼問題的解決方法

    這篇文章主要介紹了python抓取并保存html頁面時(shí)亂碼問題的解決方法,結(jié)合實(shí)例形式分析了Python頁面抓取過程中亂碼出現(xiàn)的原因與相應(yīng)的解決方法,需要的朋友可以參考下
    2016-07-07
  • Python進(jìn)程間通信Queue實(shí)例解析

    Python進(jìn)程間通信Queue實(shí)例解析

    這篇文章主要介紹了Python進(jìn)程間通信Queue實(shí)例解析,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • python根據(jù)list重命名文件夾里的所有文件實(shí)例

    python根據(jù)list重命名文件夾里的所有文件實(shí)例

    今天小編就為大家分享一篇python根據(jù)list重命名文件夾里的所有文件實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10

最新評(píng)論

开封市| 静安区| 井冈山市| 茂名市| 镇安县| 崇左市| 赤城县| 波密县| 中江县| 灵宝市| 乐昌市| 浠水县| 都江堰市| 九龙坡区| 双城市| 景宁| 彩票| 五原县| 依兰县| 临湘市| 榆树市| 六枝特区| 沙坪坝区| 栖霞市| 秦安县| 永德县| 萍乡市| 历史| 乐清市| 潢川县| 临邑县| 扶风县| 鹤峰县| 广州市| 菏泽市| 丰都县| 宜兴市| 龙岩市| 青阳县| 柳州市| 交城县|