首页 > 其他分享 >5.15

5.15

时间:2024-05-15 11:11:42浏览次数:35  
标签:canvas content oval range 5.15 fun row

  1. python实验报告三

    (一)、设计实现电子算盘,并完成测试

    题目描述

    给小朋友设计一个电子算盘。要求绘制电子算盘界面,设计并实现打珠算过程(界面参考如下图示)。

    界面右侧要求以图形绘制的方式绘制自画像,注意不能是图像文件显示的形式。

     

    【源代码程序】

    from tkinter import *

    def initWindow():
       rect = canvas.create_rectangle(25, 40, 450, 400, width=3)       # 算盘边框
       x0, y0, x1, y1 = 0, 0, 0, 0
       for i in range(5):          # 生成串算珠的线
           line_shu = canvas.create_line(70 + x0, 40 + y0, 70 + x1, 400 + y1, width=3)
           x0 += 80
           x1 += 80
       line_fenge = canvas.create_line(25, 100, 450, 100, width=3)     # 生成上下珠的分割线
       x0, y0, x1, y1 = 0, 0, 0, 0
       for i in range(5):  # 生成5个上珠
           top_oval[i] = canvas.create_oval(40 + x0, 60 + y0, 100 + x1, 90 + y1, fill='orange', tags=f"top{i}")
           x0 += 80
           x1 += 80
       x0, y0, x1, y1 = 0, 0, 0, 0
       for i in range(4):  # 生成4*5个下珠
           for j in range(5):
               below_oval[i][j] = canvas.create_oval(40 + x0, 160 + y0, 100 + x1, 190 + y1, fill='yellow', tags=f"below{i}{j}")
               chushi[i][j] = canvas.coords(below_oval[i][j])
               x0 += 80
               x1 += 80
           x0 = 0
           x1 = 0
           y0 += 60
           y1 += 60
       print(chushi)
       num = [[0 for i in range(5)] for j in range(4)]  # 五个下珠分别对应的数值
       num2 = [0 for i in range(5)]  # 五个上珠分别对应的数值
       canvas.create_oval(750, 370, 470, 90, fill='yellow')
       canvas.create_oval(500, 170, 560, 200, fill='black')
       canvas.create_oval(650, 170, 710, 200, fill='black')

       canvas.create_arc(500, 200, 700, 300, start=-150, extent=120, style=ARC, width=3)
    def get_empty():
       empty = [[0 for j in range(5)] for i in range(4)]
       for i in range(4):
           for j in range(5):
               if canvas.coords(below_oval[i][j]) != chushi[i][j]:
                   empty[i][j] = 1
       print(empty)
       return empty

    def bind():
       def handler_adaptor(handler, fun, row, col):
           """事件处理函数的适配器,相当于中介,可以帮助tag_bind函数传递参数"""
           return lambda event, handler=handler, fun=fun, col=col, row=row: handler(event=event, fun=fun, row=row, col=col)

       def handler_adaptor2(handler2, fun, row):
           """事件处理函数的适配器,相当于中介,可以帮助tag_bind函数传递参数"""
           return lambda event, handler2=handler2, fun=fun, row=row: handler2(event=event, fun=fun, row=row)

       def handler(event, fun, row, col):
           """下珠上划"""
           content = fun       # 这个就是被点击的算珠id
           empty = get_empty()
           if row == 0:
               if float(canvas.coords(content)[1]) - 40 >= 100:
                   canvas.move(content, 0, -40)
           else:
               if empty[row - 1][col] == 1:
                   if float(canvas.coords(content)[1]) - 40 >= 110 + 10 * (row + 1):
                       canvas.move(content, 0, -40)

       def handler2(event, fun, row):
           """上珠上划"""
           content = fun       # 这个就是被点击的算珠id
           if float(canvas.coords(content)[1]) - 20 >= 40:
               canvas.move(content, 0, -20)

       def handler3(event, fun, row, col):
           """下珠下划"""
           content = fun       # 这个就是被点击的算珠id
           empty = get_empty()
           if row == 3:
               if float(canvas.coords(content)[1]) + 40 <= 350:
                   canvas.move(content, 0, 40)
           else:
               if empty[row][col] == 1:
                   canvas.move(content, 0, 40)

       def handler4(event, fun, row):
           """上珠下划"""
           content = fun       # 这个就是被点击的算珠id
           if float(canvas.coords(content)[1]) + 20 <= 60:
               canvas.move(content, 0, 20)

       for i in range(5):
           canvas.tag_bind(top_oval[i], "<Button-1>", handler_adaptor2(handler2, fun=top_oval[i], row=i))
           canvas.tag_bind(top_oval[i], "<Button-3>", handler_adaptor2(handler4, fun=top_oval[i], row=i))
       for i in range(4):
           for j in range(5):
               canvas.tag_bind(below_oval[i][j], "<Button-1>", handler_adaptor(handler, fun=below_oval[i][j], row=i, col=j))
               canvas.tag_bind(below_oval[i][j], "<Button-3>", handler_adaptor(handler3, fun=below_oval[i][j], row=i, col=j))

    if __name__ == "__main__":
       window = Tk()
       window.title("电子算盘")
       window.geometry("800x500")
       canvas = Canvas(window, width="800", height="500", bg="white")
       canvas.pack()
       top_oval = [int for i in range(5)]  # 定义列表存储5个上珠
       below_oval = [[int for i in range(5)] for i in range(4)]  # 定义列表存储4*5个下珠
       chushi = [[0 for j in range(5)] for i in range(4)]  # 所有下珠的初始坐标
       initWindow()
       bind()
       window.mainloop()

标签:canvas,content,oval,range,5.15,fun,row
From: https://www.cnblogs.com/bdsz/p/18193439

相关文章

  • 5.15软工日报
    今天学习了python的图像处理,并手绘了一个算盘来进行运算definitWindow():rect=canvas.create_rectangle(25,40,450,400,width=3)#算盘边框x0,y0,x1,y1=0,0,0,0foriinrange(5):#生成串算珠的线line_shu=canvas.create......
  • qt 属性控件 使用qt提供的源码 qtpropertybrowser(D:\Qt\5.15.2\Src\qttools\src
    效果:   直接将头文件h和源文件cpp文件添加到项目中。cmakeLists.txt:file(GLOBqtpropertybrowser${QTPROPERTYBROWSER_DIR}/*.cpp${QTPROPERTYBROWSER_DIR}/*.h)include_directories("${QTPROPERTYBROWSER_DIR}")设置了源文件路径 只有一个cpp文件:#includ......
  • 统信 qt5.15.2安装
    mount挂载windows(dai)共享文件夹参考:https://www.cnblogs.com/LiuYanYGZ/p/12043945.html$cd/data/home/uos01/$mkdirwindows_share$sudomount-tcifs-ousername=share,password=share//192.168.11.111/share./windows准备要装环境的路径$cd/data/home/uos......
  • centOs 6.10 编译 qt 5.15.11.
    centOs6.10编译qt5.15.11安装依赖库xcb依赖库qtxcb需要的依赖如何要用x11,就要在编译的时候加上-xcb选项,就要安装xcb相关的库。到时可以在config.log文件查看,缺少哪个库就安装哪个。下面是我手动安装的库和对应版本:xcb-proto-1.14.tar.gzxcb-util-image-0.......
  • 全志T3(armhf) - QT5(qt5.15.10)编译与使用
    1.导入交叉编译器路径加入PATHexportPATH=$PATH:/opt/ext-toolchain/bin/我的交叉编译工具链放在/opt/ext-toolchain下ls/opt/ext-toolchainarm-linux-gnueabihfbingcc-linaro-5.3.1-2016.05-linux-manifest.txtincludeliblibexecsharels/opt/ext-toolchain......
  • 记录centos stream 9 编译qt5.15.10源码
    开始装的一些依赖库没有记录gcc之类的,都是通过dnf安装的主要是make过程中出现的问题(qtwebengine)及其如何解决编译的命令如下./configure-prefix/home/kun/usr/Qt/5.15.10-opensource-confirm-licensemake-j16makeinstallconfigure阶段失败一般都是缺少,都是dnf解决的......
  • VTK9.1.0在Windows10+VS2019+Qt 5.15.2环境下编译安装以及VTK应用于QT
    下载VTK安装包在VTK官网Download|VTK中下载VTK9.1.0待编译源码,解压后在路径Documentation/dev/bulid.md中可以看到官方提供的Prerequisites以及简易教程编译环境安装按照官方提供的Prerequisites,安装以下环境:CMakeVersion3.12ornewer,however,thelatestversionisal......
  • win 10 Qt5.15.2安装
    Qt简介截止今天,Qt长期支持的版本只有Qt5.15、Qt6.2和Qt6.5。在Qt6.5版本,在新版本中有许多类被聚合,有些函数发生改变。当然6.5也更强大了,有了更丰富的接口和函数,让使用者能更加便利和快速的开发。但是我今天所讲述的并不是Qt6.5,而是Qt5.15.2的安装。Qt安装Qt已经放......
  • 5.15 static 应用案例
    classBook{privateStringtitle;privatestaticintcount=0;publicBook(){//无参构造this("NOTITLE-"+count++);//避免了没有title的情况,调用下面的构造方法,把“NOTITLE-count”当作title}publicBook(Stringtitle){......
  • qt5.15.9 静态编译 msvc 2017
    软件准备:VisualStudio2017ActivePerlPythonopenssl1.1以上版本QT5.15.9源码: https://download.qt.io/archive/qt/5.15/5.15.9/single/ 第一步命令:D:\qt-everywhere-src-5.15.9>configure.bat-prefixD:\Qt\Qt5.15.9-static-static-static-runtime-confirm-li......