最终的部分运行效果图(程序见序号4):
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# ------------------------------------------------------------
#
# 参考资料:
# 用 python 和 tkinter 做简单的窗口视窗 - 网易云课堂
# https://study.163.com/course/courseLearn.htm?courseId=1003216011#/learn/video?lessonId=1003650346&courseId=1003216011
#
# ------------------------------------------------------------
# ******************** class8_Canvas 画布 *******************
# ******************** class8_Canvas 画布 *******************
# class8_Canvas 画布
# =====>>>>>>内容概览
# =====>>>>>>内容概览
'''
# ------------------------------------------------------------
# # 1、生成一个画布
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 2、画布里头放置图片
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 3、画布里头放置 直线 + 圆形 + 矩形
# ------------------------------------------------------------
# ------------------------------------------------------------
# # 4、画布里头放置 矩形
# ------------------------------------------------------------
'''
# ------------------------------------------------分割线-------------------------------------------------
# ------------------------------------------------分割线-------------------------------------------------
# ------------------------------------------------分割线-------------------------------------------------
# ------------------------------------------------分割线------------------------------------------------- ''' # ------------------------------------------------------------ # # 1、生成一个画布 # ------------------------------------------------------------ ''' # import tkinter as tk # # window = tk.Tk() # window.title("class8_Canvas 画布") # window.geometry("500x600") # # canvas = tk.Canvas(window, bg='blue', height=200, width=300) # canvas.pack() # # window.mainloop() # ------------------------------------------------分割线------------------------------------------------- ''' # ------------------------------------------------------------ # # 2、画布里头放置图片 # # # image = canvas.create_image(100, 0,image=image_file) # # # 100: x方向 # # # 0 : Y方向 # # # 默认锚点为中心 # ------------------------------------------------------------ ''' # import tkinter as tk # # window = tk.Tk() # window.title("class8_Canvas 画布") # window.geometry("500x600") # # canvas = tk.Canvas(window, bg='blue', height=200, width=300) # image_file = tk.PhotoImage(file=r'D:C_cachepy kinter\_2-1.gif') # 图片的格式只能是gif, GIF and PGM/PPM # # image = canvas.create_image(100, 0,image=image_file) # 不设置锚点 # image = canvas.create_image(100, 0, anchor='nw',image=image_file) # 设置锚点为左上角 # # canvas.pack() # # window.mainloop() # ------------------------------------------------分割线------------------------------------------------- ''' # ------------------------------------------------------------ # # 3、画布里头放置 直线 + 圆形 + 矩形 # ------------------------------------------------------------ ''' # import tkinter as tk # # window = tk.Tk() # window.title("class8_Canvas 画布") # window.geometry("500x600") # # canvas = tk.Canvas(window, bg='blue', height=200, width=300) # x0,y0,x1,y1 = 0, 0, 100, 50 # 设定 # # 画直线, 矩形左上角(起点):x0, y0; 矩形的右下角(终点):x1, y1 # line = canvas.create_line(x0, y0, x1, y1) # # 画圆形, 矩形左上角(起点):x0+50, y0; 矩形的右下角(终点):x1, y1 # oval = canvas.create_oval(x0+50, y0, x1, y1, fill='red') # # 画矩形, 矩形左上角(起点):x0+50, y0; 矩形的右下角(终点):x1+50, y1+50 # rect = canvas.create_rectangle(x0+50, y0, x1+50, y1+50) # canvas.pack() # # window.mainloop() # ------------------------------------------------分割线------------------------------------------------- ''' # ------------------------------------------------------------ # # 4、画布里头放置 矩形,矩形可以被移动 # ------------------------------------------------------------ ''' # import tkinter as tk # # def moveit(): # canvas.move(rect,2,0) # # window = tk.Tk() # window.title("class8_Canvas 画布") # window.geometry("500x600") # # # canvas = tk.Canvas(window, bg='blue', height=200, width=300) # # x0,y0,x1,y1 = 0, 0, 100, 50 # 设定 # # 画矩形, 矩形左上角(起点):x0+50, y0; 矩形的右下角(终点):x1+50, y1+50 # rect = canvas.create_rectangle(x0+50, y0, x1+50, y1+50) # canvas.pack() # # # 设置按钮(如果放置在) canvas.pack() 的上面的话,那么按钮的位置在画布的上面 # b = tk.Button(window, text='move', command=moveit) # b.pack() # window.mainloop()
最终的部分运行效果图(程序见序号4):