python实验报告3可视化
所花时间:2小时
代码量:如下
博客量:本学期截至目前59篇
了解到的知识点:python的可视化
一实验目的
l 使学生综合运用图形用户界面设计的概念;
l 使学生熟悉使用中间面板,组成层次复杂的GUI界面;
l 使学生掌握Python图形绘制和图像处理步骤与方法;
l 使学生掌握Python可视化处理的步骤、方法与编程;
二实验环境及实验准备
l 所需硬件环境为微机;
l 所需软件环境为Python 3.X等;
l 掌握Python下界面容器与基本组件的基本知识与应用;
l 掌握Python下事件处理模型;
l 掌握Python下图形绘制的方法;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | from tkinter import * tk = Tk() tk.title( "电子算盘" ) # 窗口名称 tank = Canvas(tk, width= 1000 , height= 600 , bg= 'ivory' ) # 创建画板 tank.pack() # 显示画板 tank.create_rectangle( 30 , 30 , 520 , 190 , width= 3 ) # 左上侧方框 tank.create_rectangle( 30 , 190 , 520 , 570 , width= 3 ) # 左下侧方框 tank.create_oval( 900 , 400 , 620 , 120 , fill= 'yellow' ) tank.create_oval( 800 , 200 , 850 , 250 , fill= 'black' , tags= 'left' ) tank.create_oval( 670 , 200 , 720 , 250 , fill= 'black' , tags= 'right' ) tank.create_line( 695 , 320 , 825 , 320 , width= 5 , tags= 'mouth' ) backround_image = PhotoImage(file= "3.png" ) # 上珠图片 backround_image2 = PhotoImage(file= "4.png" ) # 下珠图片 button = Button() button1 = [button for i in range( 5 )] # 5 个上珠 button2 = [[button for i in range( 5 )] for i in range( 4 )] # 四行,每行五个下珠 num = [[ 0 for i in range( 5 )] for i in range( 4 )] # 五个下珠分别对应的数值 num2 = [ 0 for i in range( 5 )] # 五个上珠分别对应的数值 def getNum(num, num2): # 计算算盘总和 sum_ = 0 for i in num: for j in i: sum_ += j for i in num2: sum_ += i return sum_ def button_click_back(events): # 鼠标右击点击事件触发 widget = events.widget for i in range( 5 ): if widget == button1[i]: button1[i].place(x= 40 + 100 * i, y= 50 + 70 * 1 ) num2[i] = 0 label = Label(tk, text= "当前数字:" + str(getNum(num, num2)), width= 30 , height= 4 ) label.place(x= 780 , y= 30 ) for i in range( 4 ): for j in range( 5 ): if widget == button2[i][j]: if i == 3 : button2[ 3 ][j].place(x= 40 + 100 * j, y= 210 + 70 * (i + 1 )) num[ 3 ][j] = 0 label = Label(tk, text= "当前数字:" + str(getNum(num, num2)), width= 30 , height= 4 ) label.place(x= 780 , y= 30 ) if i == 2 : button2[ 2 ][j].place(x= 40 + 100 * j, y= 210 + 70 * (i + 1 )) button2[ 3 ][j].place(x= 40 + 100 * j, y= 210 + 70 * (i + 2 )) num[ 2 ][j] = 0 num[ 3 ][j] = 0 label = Label(tk, text= "当前数字:" + str(getNum(num, num2)), width= 30 , height= 4 ) label.place(x= 780 , y= 30 ) if i == 1 : button2[ 1 ][j].place(x= 40 + 100 * j, y= 210 + 70 * (i + 1 )) button2[ 2 ][j].place(x= 40 + 100 * j, y= 210 + 70 * (i + 2 )) button2[ 3 ][j].place(x= 40 + 100 * j, y= 210 + 70 * (i + 3 )) num[ 1 ][j] = 0 num[ 2 ][j] = 0 num[ 3 ][j] = 0 label = Label(tk, text= "当前数字:" + str(getNum(num, num2)), width= 30 , height= 4 ) label.place(x= 780 , y= 30 ) if i == 0 : button2[ 0 ][j].place(x= 40 + 100 * j, y= 210 + 70 * (i + 1 )) button2[ 1 ][j].place(x= 40 + 100 * j, y= 210 + 70 * (i + 2 )) button2[ 2 ][j].place(x= 40 + 100 * j, y= 210 + 70 * (i + 3 )) button2[ 3 ][j].place(x= 40 + 100 * j, y= 210 + 70 * (i + 4 )) num[ 0 ][j] = 0 num[ 1 ][j] = 0 num[ 2 ][j] = 0 num[ 3 ][j] = 0 label = Label(tk, text= "当前数字:" + str(getNum(num, num2)), width= 30 , height= 4 ) label.place(x= 780 , y= 30 ) def button_click(events): # 鼠标左击点击事件触发 widget = events.widget for i in range( 5 ): if widget == button1[i]: button1[i].place(x= 40 + 100 * i, y= 50 + 70 * 0 ) num2[i] = 10 ** ( 4 - i) * 5 label = Label(tk, text= "当前数字:" + str(getNum(num, num2)), width= 30 , height= 4 ) label.place(x= 780 , y= 30 ) for i in range( 4 ): for j in range( 5 ): if widget == button2[i][j]: if i == 3 : button2[ 0 ][j].place(x= 40 + 100 * j, y= 210 + 70 * (i - 3 )) button2[ 1 ][j].place(x= 40 + 100 * j, y= 210 + 70 * (i - 2 )) button2[ 2 ][j].place(x= 40 + 100 * j, y= 210 + 70 * (i - 1 )) button2[ 3 ][j].place(x= 40 + 100 * j, y= 210 + 70 * (i)) num[ 0 ][j] = 10 ** ( 4 - j) * 1 num[ 1 ][j] = 10 ** ( 4 - j) * 1 num[ 2 ][j] = 10 ** ( 4 - j) * 1 num[ 3 ][j] = 10 ** ( 4 - j) * 1 label = Label(tk, text= "当前数字:" + str(getNum(num, num2)), width= 30 , height= 4 ) label.place(x= 780 , y= 30 ) if i == 2 : button2[ 0 ][j].place(x= 40 + 100 * j, y= 210 ) button2[ 1 ][j].place(x= 40 + 100 * j, y= 210 + 70 * 1 ) button2[ 2 ][j].place(x= 40 + 100 * j, y= 210 + 70 * 2 ) num[ 0 ][j] = 10 ** ( 4 - j) * 1 num[ 1 ][j] = 10 ** ( 4 - j) * 1 num[ 2 ][j] = 10 ** ( 4 - j) * 1 label = Label(tk, text= "当前数字:" + str(getNum(num, num2)), width= 30 , height= 4 ) label.place(x= 780 , y= 30 ) if i == 1 : button2[ 0 ][j].place(x= 40 + 100 * j, y= 210 ) button2[ 1 ][j].place(x= 40 + 100 * j, y= 210 + 70 * 1 ) num[ 0 ][j] = 10 ** ( 4 - j) * 1 num[ 1 ][j] = 10 ** ( 4 - j) * 1 label = Label(tk, text= "当前数字:" + str(getNum(num, num2)), width= 30 , height= 4 ) label.place(x= 780 , y= 30 ) else : button2[i][j].place(x= 40 + 100 * j, y= 210 + 70 * i) num[ 0 ][j] = 10 ** ( 4 - j) * 1 label = Label(tk, text= "当前数字:" + str(getNum(num, num2)), width= 30 , height= 4 ) label.place(x= 780 , y= 30 ) for i in range( 5 ): # 生成 5 个上珠 button1[i] = Button(tk, image=backround_image) button1[i].bind( "<Button-1>" , button_click) button1[i].bind( "<Button-3>" , button_click_back) button1[i][ "bg" ] = "ivory" button1[i][ "border" ] = "0" button1[i].place(x= 40 + 100 * i, y= 50 + 70 ) for i in range( 4 ): # 四行,每行生成 5 个下珠 for j in range( 5 ): button2[i][j] = Button(tk, image=backround_image2) button2[i][j].bind( "<Button-1>" , button_click) button2[i][j].bind( "<Button-3>" , button_click_back) button2[i][j][ "bg" ] = "ivory" button2[i][j][ "border" ] = "0" button2[i][j].place(x= 40 + 100 * j, y= 210 + 70 * (i + 1 )) tk.mainloop() 第一题源代码 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)