GUI的最终选择Tkinter模块练习篇

一、Canvas画布练习

1)简单的绘制图框

from tkinter import *
# 构建一个窗口
tk = Tk()
# 画布
canvas= Canvas(tk,width=400,height=400) # 单位像素
canvas.pack()
def python():   # 槽函数
    print("very good")
btn = Button(tk,text="oldboyedu",command=python)
# 显示窗口
btn.pack()

canvas.create_arc(20,20,100,50,extent=145,style=ARC)
 #  20    20     100        50           145
 #  x轴  y轴    x走了100   y走了50    角度145
canvas.create_rectangle(10,10,50,50,fill="green")
canvas.create_polygon(200,30,240,40,120,100,150,120,fill="red",outline="black")

# 运行窗口
tk.mainloop()
View Code

 2)循环随机绘制图框

from tkinter import *
import random
# 构建一个窗口
tk = Tk()
# 画布
canvas= Canvas(tk,width=400,height=400) # 单位像素
canvas.pack()

myColor = ["red","orange","yellow","green","cyan","blue","purple"]

def random_rectangle(width,height,myColor):
    x = random.randrange(width)
    y = random.randrange(width)
    x1 = x + random.randrange(height)
    y1 = y + random.randrange(height)
    canvas.create_rectangle(x,y,x1,y1,fill=myColor,stipple="gray12",outline=myColor,dash=10)
    # 循环200个随机矩形
for nums in range(100):
    random_rectangle(200,200,myColor[nums%7])

tk.mainloop()
View Code

 

转载于:https://www.cnblogs.com/linu/p/9180556.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值