首页 > 编程语言 >8-1 【Python0021】电子算盘

8-1 【Python0021】电子算盘

时间:2024-05-22 09:18:08浏览次数:24  
标签:Python0021 30 电子 40 button2 num place 100 算盘

from tkinter import *

 

class PaintApp:

    def __init__(self, master):

        self.master = master

        self.canvas = tank

        

        self.canvas.pack(fill=X)

        self.canvas.bind('<B1-Motion>', self.draw)

    

    def draw(self, event):

        canvas_half_width = 550

        if event.x > canvas_half_width:

          x, y = event.x, event.y

          self.canvas.create_oval(x-2, y-2, x+2, y+2, fill='black')

    

    def save(self):

        self.canvas.postscript(file='selfie.eps', colormode='color')

        img = Image.open('selfie.eps')

        img.save('selfie.png', 'png')

 

root = Tk()

root.title("电子算盘")  # 窗口名称

tank = Canvas(root, width=1000, height=600, bg='ivory')  # 创建画板

tank.pack(fill=X)  # 显示画板

tank.create_rectangle(30, 30, 520, 190, width=3)  # 左上侧方框

tank.create_rectangle(30, 190, 520, 570, width=3)  # 左下侧方框

 

 

app = PaintApp(root)

 

save_button = Button(root, text='Save', command=app.save)

save_button.pack()

 

backround_image = PhotoImage(file=".vscode\images\orange2.png")  # 上珠图片

backround_image2 = PhotoImage(file=".vscode\images\yellow2.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(root, 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(root, 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(root, 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(root, 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(root, 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(root, 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(root, 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(root, 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(root, 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(root, text="当前数字:" + str(getNum(num, num2)), width=30, height=4)

                    label.place(x=780, y=30)

 

 

for i in range(5):  # 生成5个上珠

    button1[i] = Button(root, 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(root, 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))

 

root.mainloop()

![image.png]({PTA_URL}/api/private-image?p=user-uploads/1566653959899246592/2024-5-15/1715738882-eca8cbd4-e9bc-4225-86a3-562266d45edd.png)

标签:Python0021,30,电子,40,button2,num,place,100,算盘
From: https://www.cnblogs.com/galileo9527/p/18205470

相关文章

  • 实验二 电子传输系统安全-进展1
    小组成员肘击大苞米20211205周翔20211228许博文20211223旦增赤列上周任务完成情况(代码链接,所写文档等)电子公文传输系统重新调试:成功将上学期的HTML系统转换为Python,满足新学期任务需求哈希存储用户口令并加盐:已实现,可切换至SHA-256或SM2算法。数据库中无明文密码,完成了登录......
  • 实验二 电子传输系统安全-进展1
    -上周任务完成情况(代码链接,所写文档等)一、制作web界面 二、构建对应的数据库三、导入bouncycastle -本周计划实现对用户名和密码的SM3加密且加盐对应代码已上传至gitee赖加奕/gongwenchuanshu(gitee.com)......
  • 电子传输系统安全-进展1
    实验二电子传输系统安全-进展1上周任务完成情况完成了上学期电子公文传输系统的重新调试通过部署了bouncycastle学习了bouncycastle将jar包添加到依赖项本周计划将上学期电子公文传输系统重新调试通过部署bouncycastle学习bouncycastle将jar包添加到依赖项参考链......
  • 实验二 电子传输系统安全-进展1
    实验二电子传输系统安全-进展1上周任务完成情况(代码链接,所写文档等)本周计划上周任务完成情况(代码链接,所写文档等)将上学期电子公文传输系统重新调试通过部署bouncycastle学习bouncycastle将jar包添加到依赖项在pom.xml文件中引入BouncyCastle的包<dependency>......
  • 实验二 电子传输系统安全-进展1
    实验二电子传输系统安全-进展1上周任务找到上学期电子公文传输系统并重新调试通过学习加密相关知识,即gmssl部署安装gmssl使用gmssl测试证书调试运行加装gmssl后的系统上周任务完成情况学号姓名任务完成情况宁心宇找到上学期电子公文传输系统并重新调试通过......
  • 实验二 电子传输系统安全-进展1
    实验二电子传输系统安全-进展1上周任务完成情况(代码链接,所写文档等)本周计划上周任务完成情况(代码链接,所写文档等)将上学期电子公文传输系统重新调试通过部署bouncycastle学习bouncycastle将jar包添加到依赖项在pom.xml文件中引入BouncyCastle的包<dependency>......
  • 实验二 电子传输系统安全-进展1
    上周任务完成情况(代码链接,所写文档等将电子公文传输系统重新调试通过,并运行小组成员每个人读完《Core.Software.Security.Security.at.the.Source.CN.软件安全.从源头开始》和《The.Security.Development.Lifecycle.CN.软件安全开发生命周期》,并写好相应的读书笔记熟悉运行之......
  • 电子公文传输系统安全-进展一
    上周任务完成情况每人撰写并提交一份读书笔记小组撰写并提交一份加固计划书、一份安全性设计方案小组撰写并提交一份系统安全设计报告小组讨论原公文传输系统的问题不足和改进方向小组讨论新公文传输系统的修改方案和预期效果每人自行学习国密标准、算法知识和设计方法原......
  • 20211314 电子政务及安全研究报告
    任务详情通过微信读书、cnki、密码行业标准化技术委员会网站等途径查找电子政务及安全相关内容,列出你查找的资料综述解电子政务的内容和网络规范的内容综述政务应急平台的内容综述电子政务安全的内容综述电子政务安全管理的内容综述电子政务信息安全等级保护、电子政务认证......
  • 电子书自由:python生成epub电子书
    前言最近在看《剑来》小说,但是kindle在线看不方便,而且我在网上找到的只有600多章,目前最新已经更新到了1200章,为了确保后面有书可读,同时也为了巩固下python技能,于是我找到一个免费的网站,然后通过python获取书籍内容,最终将获取到的内容,生成epub电子书,核心内容如下:获取电子书的内......