TkinterCanvas相关函数用法-创新互联
Tkinter Canvas相关函数用法
当前文章:TkinterCanvas相关函数用法-创新互联
URL地址:http://myzitong.com/article/igepc.html
- 一、create_rectangle函数用法
- 生成给定坐标的矩形
- 二、create_line函数用法
- 生成给定坐标的线段
- 三、coords函数用法
- 获取图形块的左下和右上的水平坐标
- 四、move函数用法
- 移动图形块
from tkinter import *
import numpy as np
def main():
root = Tk()
w = Canvas(
root,
width=200,
height=200,
background="white"
)
w.pack()
blue_rect = w.create_rectangle(50, 50, 100, 100, fill='blue')
# 生成左下坐标为[50, 50],右上坐标为[100, 100]的蓝色矩形
mainloop()
if __name__ == '__main__':
main()
运行结果如下:
from tkinter import *
import numpy as np
def main():
root = Tk()
w = Canvas(
root,
width=200,
height=200,
background="white"
)
w.pack()
yellow_line = w.create_line(0, 0, 200, 200, fill='yellow')
# 生成起点为[0, 0],终点为[200, 200]的黄色线段
mainloop()
if __name__ == '__main__':
main()
运行结果如下:
示例代码如下
from tkinter import *
import numpy as np
def main():
root = Tk()
w = Canvas(
root,
width=200,
height=200,
background="white"
)
w.pack()
blue_rect = w.create_rectangle(50, 50, 100, 100, fill='blue')
a = np.array(w.coords(blue_rect))
# 获得蓝色矩形的坐标
b = np.array(w.coords(blue_rect)[:2])
# 获得蓝色矩形的前两个坐标
print(a)
# a = [ 50. 50. 100. 100.]
print(b)
# b = [50. 50.]
mainloop()
if __name__ == '__main__':
main()
四、move函数用法
移动图形块from tkinter import *
import numpy as np
def main():
root = Tk()
w = Canvas(
root,
width=200,
height=200,
background="white"
)
w.pack()
blue_rect = w.create_rectangle(50, 50, 100, 100, fill='blue')
red_rect = w.create_rectangle(100, 100, 150, 150, fill='red')
w.move(blue_rect, -50, -50)
# 移动蓝色矩形,将矩形的左下坐标[-50,-50]
w.move(red_rect, 50, 50)
# 移动红色矩形,保持矩形形状不变,将红色矩形的左下坐标加[50, 50]
a = np.array(w.coords(blue_rect))
# 获得移动后蓝色矩形的坐标
b = np.array(w.coords(red_rect))
# 获得移动后红色矩形的坐标
print(a)
# [ 0. 0. 50. 50.]
print(b)
# [150. 150. 200. 200.]
mainloop()
if __name__ == '__main__':
main()
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
当前文章:TkinterCanvas相关函数用法-创新互联
URL地址:http://myzitong.com/article/igepc.html