Python?tkinter庫(kù)繪圖實(shí)例分享
一、小房子繪制
實(shí)例代碼:
# coding=utf-8 import tkinter as tk ? ? ?# 導(dǎo)入tkinter模塊 ? root = tk.Tk() ? ? ? ? ? ?# 創(chuàng)建一個(gè)頂級(jí)窗口 root.title('小房子1') ? ? # 設(shè)置標(biāo)題 canvas = tk.Canvas(root, bg='white', width=700, height=700) ? # 在root窗口上創(chuàng)建畫布canvas,白色背景,寬和高均為700像素 canvas.pack(anchor='center') ? # canvas在root上居中顯示 ? points = [(50, 250), (350, 50), (650, 250)] ? # 三角形頂點(diǎn)坐標(biāo)位置 canvas.create_polygon(points, fill='gray', outline='black', width=10) ? # 白色填充,紅色線條,線寬為10 canvas.create_rectangle((200, 250, 500, 550), ? ? ? ? ? ? ? ? ? ? ? ? fill='white', outline='black', width=10) ? ? # 繪制矩形,白色填充,綠色線條,線寬為10 canvas.create_oval((250, 300, 450, 500), ? ? ? ? ? ? ? ? ? ?fill='purple', outline='black', width=10) ? ?# 繪制圓形,黃色填充,黃色線條,線寬為10 ? root.mainloop() ? # 進(jìn)入消息循環(huán)
運(yùn)行結(jié)果:
二、彩色氣泡動(dòng)畫繪制
實(shí)例代碼:
#coding=utf-8 import tkinter as tk import random as rd import time # 全局變量,全部為list對(duì)象 # 分別為:x方向速度,y方向速度,半徑,位置,圖形標(biāo)記 speedXList, speedYList, rList, posList, idList = [], [], [], [], [] # 可選的顏色 colorList = ['pink', 'gold', 'lightblue', 'lightgreen', 'silver'] # 畫布的寬度、高度,以及圖形個(gè)數(shù) width, height, num = 400, 400, 5 root = tk.Tk() # 創(chuàng)建和布局畫布 canvas = tk.Canvas(root, width=width, height=height, background='white') canvas.pack() ? for i in range(num): ?? ?# 隨機(jī)產(chǎn)生圖形初始位置 ? ? x = rd.randint(100, width - 100) ? ? y = rd.randint(100, height - 100) ? ? # 添加到圖形位置列表 ? ? posList.append((x, y)) ? ? # 隨機(jī)產(chǎn)生半徑,并添加到半徑列表 ? ? r = rd.randint(20, 50) ? ? rList.append(r) ? ? # 隨機(jī)選取一種顏色 ? ? color = rd.sample(colorList, 1) ? ? # 創(chuàng)建一個(gè)橢圓/圓,用選定的顏色填充 ? ? id = canvas.create_oval(x - r, y - r, x + r, y + r, ? ? ? ? ? ? ? ? ? ? ? ? ? ? fill=color, outline=color) ? ? # 保存圖形標(biāo)識(shí) ? ? idList.append(id) # 設(shè)置隨機(jī)的移動(dòng)速度,并保存 ? ? speedXList.append(rd.randint(-10, 10)) ? ? speedYList.append(rd.randint(-10, 10)) ? while True: ? ? for i in range(num): ? ? ? ? # 圖形當(dāng)前所在位置 ? ? ? ? item = posList[i] ? ? ? ? r = rList[i] ? ? ? ? ?# 如果x位置超過邊界,則改編x速度方向 ? ? ? ? if item[0] - r < 0 or item[0] + r > width: ? ? ? ? ? ? speedXList[i] = -speedXList[i] ? ? ? ? # 如果y位置超過邊界,則改編y速度方向 ? ? ? ? if item[1] - r < 0 or item[1] + r > height: ? ? ? ? ? ? speedYList[i] = -speedYList[i] ? ? ? ? # 按照當(dāng)前的速度計(jì)算下新的位置 ? ? ? ? posList[i] = (item[0] + speedXList[i], item[1] + speedYList[i]) ? ? ? ? x, y = posList[i][0], posList[i][1] ? ? ? ? # 移動(dòng)到新的位置 ? ? ? ? canvas.coords(idList[i], (x - r, y - r, x + r, y + r)) ? ? ? ? # 刷新畫面 ? ? ? ? canvas.update() ? ? # 等待0.1秒,即每秒鐘更新10幀,形成動(dòng)畫 ? ? time.sleep(0.1)
運(yùn)行結(jié)果:
三、畫布創(chuàng)建
實(shí)例代碼:
import tkinter as tk ? ? ? ? ? # 導(dǎo)入tkinter庫(kù),并重命名為tk mywindow = tk.Tk() ? ? ? ? ? ? # 創(chuàng)建一個(gè)窗體 mywindow.title("我是一個(gè)畫布") ? ? ?# 設(shè)置窗體的標(biāo)題 mycanvas = tk.Canvas(mywindow, width=400, height=300, bg="purple") ?# 創(chuàng)建畫布并布局 ? mycanvas.pack() mywindow.mainloop() ? ? ?# 顯示畫布
運(yùn)行結(jié)果:
到此這篇關(guān)于Python tkinter庫(kù)繪圖實(shí)例分享的文章就介紹到這了,更多相關(guān)tkinter庫(kù)繪圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Jupyter Notebook添加代碼自動(dòng)補(bǔ)全功能的實(shí)現(xiàn)
這篇文章主要介紹了Jupyter Notebook添加代碼自動(dòng)補(bǔ)全功能的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01使用Python監(jiān)控文件內(nèi)容變化代碼實(shí)例
在python中文件監(jiān)控主要有兩個(gè)庫(kù),一個(gè)是pyinotify,一個(gè)是watchdog。pyinotify依賴于Linux平臺(tái)的inotify,今天我們就來(lái)探討下pyinotify.2018-06-06python實(shí)現(xiàn)文件+參數(shù)發(fā)送request的實(shí)例代碼
這篇文章主要介紹了python實(shí)現(xiàn)文件+參數(shù)發(fā)送request的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01Django objects的查詢結(jié)果轉(zhuǎn)化為json的三種方式的方法
這篇文章主要介紹了Django objects的查詢結(jié)果轉(zhuǎn)化為json的三種方式的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來(lái)看看吧2018-11-11