import random from UI import * # import tkinter as tk class MEM: def __init__(self): # 绘制存储器背景板 round_rectangle(597, 205, 928, 670, r=25, fill=BACK, width=0) round_rectangle(675, 275, 910, 660, r=25, fill=BLACK, width=0) def mark(self, cv:Canvas): for i in range(20): # 如果SimMARK中该存储单元为False,则非高亮显示 if cpu.SimMARK[i] == False: cv.create_rectangle(810, 282 + 18.5 * i, 897, 298 + 18.5 * i, width=3, outline=MEMBACK) for i in range(20): # 如果SimMARK中该存储单元为True,则高亮显示 if cpu.SimMARK[i] == True: cv.create_rectangle(810, 282 + 18.5 * i, 897, 298 + 18.5 * i, width=3, outline=MEMMARK) cv.update() # 根据当前的存储数据绘制当前的存储器 def show(self, cv: Canvas, root): # 标记cpu各部分的名称 cv.create_text(670, 219, text='Mcontent', font=('微软雅黑', 12, 'bold'), anchor=NW, fill=WORD) cv.create_text(611, 356, text='Maddr', font=('微软雅黑', 12, 'bold'), anchor=NW, fill=WORD) cv.create_text(690, 255, text='地址(编号)', font=('time', 12,), anchor=NW, fill='#ffd700') cv.create_text(830, 255, text='在选单元', font=('time', 12,), anchor=NW, fill='#ffd700') # 根据存储的数据显示当前Maddr的值 cv.create_rectangle(619, 383, 648, 541, fill=WHITE, width=0) cv.create_text(620, 390, text=cpu.MAD(), font=('微软雅黑', 12), angle=90, anchor=NE, fill=BLACK) # 根据存储的数据显示当前Mcotent的值 cv.create_rectangle(753, 218, 910, 242, fill=WHITE, width=0) cv.create_text(830, 230, text=cpu.MCO(), fill=BLACK, font=('微软雅黑', 12)) # 根据存储的数据显示当前Memory中各个存储单元的值 cv.create_rectangle(685, 281, 791, 647, fill=BLACK) for i in range(20):# 显示存储器各个单元的地址 cv.create_text(688, 282 + 18.5 * i, text="00000000 " + bin(i).replace('0b', '').zfill(8), font=('微软雅黑', 8), anchor=NW, fill=GOLD) cv.create_rectangle(808, 281, 897, 649, fill=MEMBACK, width=0) for i in range(20):# 显示存储器各个单元的内容 cv.create_text(852, 290 + 18.5 * i, text=cpu.SimMem[i], font=('微软雅黑', 6), anchor=CENTER, fill=MEMWORD) self.mark(cv)# 更新画布 # 根据当前的存储数据绘制当前的存储器 def init(self, cv:Canvas, root): # 标记cpu各部分的名称 cv.create_text(670, 219, text='Mcontent', font=('微软雅黑', 12, 'bold'), anchor=NW, fill=WORD) cv.create_text(611, 356, text='Maddr', font=('微软雅黑', 12, 'bold'), anchor=NW, fill=WORD) cv.create_text(690, 255, text='地址(编号)', font=('time', 12, ), anchor=NW, fill='#ffd700') cv.create_text(830, 255, text='在选单元', font=('time', 12, ), anchor=NW, fill='#ffd700') # 根据存储的数据显示当前Maddr的值 cv.create_rectangle(619, 383, 648, 541, fill=WHITE, width=0) cv.create_text(620, 390, text=cpu.MAD(), font=('微软雅黑', 12), angle=90, anchor=NE, fill=BLACK) # 根据存储的数据显示当前Mcotent的值 cv.create_rectangle(753, 218, 910, 242, fill=WHITE, width=0) cv.create_text(830, 230, text=cpu.MCO(), fill=BLACK, font=('微软雅黑', 12)) # 根据存储的数据显示当前Memory中各个存储单元的值 # 显示存储器各个单元的地址 cv.create_rectangle(685, 281, 791, 647, fill=BLACK) for i in range(20): cv.create_text(688, 282 + 18.5 * i, text="00000000 " + bin(i).replace('0b', '').zfill(8), font=('微软雅黑', 8), anchor=NW, fill=GOLD) # 显示存储器各个单元的内容 cv.create_rectangle(808, 281, 897, 649, fill=MEMBACK, width=0) for i in range(20): cv.create_text(852, 290 + 18.5 * i, text=cpu.SimMem[i], font=('微软雅黑', 6), anchor=CENTER, fill=MEMWORD) # 更新画布 self.mark(cv) if __name__ == "__main__": mem = MEM()#实例化 mem.show(cv, root)#显示存储器界面 bw, bh = 150, 45 # 按钮1 bx1, by1 = 750, 95 B1 = tk.Button(root, text='添加程序', background=BLUE, command=lambda: test(), state=NORMAL) B1.place(x=bx1, y=by1, width=bw, height=bh) def test(): for i in range(20): time.sleep(cpu.time) for j in range(20): cpu.SimMARK[j] = False cpu.SimMARK[random.randint(0,19)] = True mem.mark(cv) cv.update() def left1(event): print(event.x,event.y) cv.bind('', left1) root.mainloop()