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')
# 生成左下坐標(biāo)為[50, 50],右上坐標(biāo)為[100, 100]的藍(lán)色矩形
mainloop()
if __name__ == '__main__':
main()
運(yùn)行結(jié)果如下:
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')
# 生成起點(diǎn)為[0, 0],終點(diǎn)為[200, 200]的黃色線(xiàn)段
mainloop()
if __name__ == '__main__':
main()
運(yùn)行結(jié)果如下:
示例代碼如下
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))
# 獲得藍(lán)色矩形的坐標(biāo)
b = np.array(w.coords(blue_rect)[:2])
# 獲得藍(lán)色矩形的前兩個(gè)坐標(biāo)
print(a)
# a = [ 50. 50. 100. 100.]
print(b)
# b = [50. 50.]
mainloop()
if __name__ == '__main__':
main()
四、move函數(shù)用法
移動(dòng)圖形塊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)
# 移動(dòng)藍(lán)色矩形,將矩形的左下坐標(biāo)[-50,-50]
w.move(red_rect, 50, 50)
# 移動(dòng)紅色矩形,保持矩形形狀不變,將紅色矩形的左下坐標(biāo)加[50, 50]
a = np.array(w.coords(blue_rect))
# 獲得移動(dòng)后藍(lán)色矩形的坐標(biāo)
b = np.array(w.coords(red_rect))
# 獲得移動(dòng)后紅色矩形的坐標(biāo)
print(a)
# [ 0. 0. 50. 50.]
print(b)
# [150. 150. 200. 200.]
mainloop()
if __name__ == '__main__':
main()
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧