You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

350 lines
18 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

from UI import *
# import tkinter as tk
class ALU:
def __init__(self):
round_rectangle(74, 48, 533, 415, r=25, fill=BACK, width=0) # 绘制ALU蓝色背景
Point = [90, 190, 120, 190, 130, 215, 205, 215, 215, 190, 245, 190, 220, 270, 115, 270]
cv.create_polygon(Point, fill=BLACK) # 绘制运算器
self.line_init()# 在图中标记各个部件的名字
def show(self):
lines = ['ALU_R1', 'ALU_R2', 'ALU_R3', 'ALU_R4', 'R1_ALU1', 'R2_ALU1',
'R3_ALU1', 'R4_ALU1', 'R1_ALU2', 'R2_ALU2', 'R3_ALU2', 'R4_ALU2']
for l in lines:
if cpu.ALUio[l] == False: #如果ALUio中该线为False则非高亮显示
eval('self.' + l + '(cv, 0)')
for l in lines:
if cpu.ALUio[l] == True: #如果ALUio中该线为True则高亮显示
eval('self.' + l + '(cv, 1)')
if cpu.ALUop['+'] == True:
self.opa(cv)
if cpu.ALUop['-'] == True:
self.opb(cv)
if cpu.ALUop['*'] == True:
self.opc(cv)
if cpu.ALUop['/'] == True:
self.opd(cv)
def line_init(self):
cv.create_text(140, 183, text='ALU', font=('微软雅黑', 20), anchor=NW, fill=WORD)
cv.create_text(345, 95, text="R1", font=('微软雅黑', 18), fill=WORD)
cv.create_text(345, 185, text="R2", font=('微软雅黑', 18), fill=WORD)
cv.create_text(345, 275, text="R3", font=('微软雅黑', 18), fill=WORD)
cv.create_text(345, 365, text="R4", font=('微软雅黑', 18), fill=WORD)
lines = ['ALU_R1', 'ALU_R2', 'ALU_R3', 'ALU_R4', 'R1_ALU1', 'R2_ALU1',
'R3_ALU1', 'R4_ALU1', 'R1_ALU2', 'R2_ALU2', 'R3_ALU2', 'R4_ALU2' ]
for l in lines:
eval('self.' + l + '(cv, 0)')
# for l in lines:
# eval('self.' + l + '(cv, -1)')
def mark(self, lines):
for i in range(5):
if i:
time.sleep(cpu.time)
if i % 2:
for l in lines:
# print('self.' + l + '(cv, 1)')
# self.init()
eval('self.' + l + '(cv, 1)')
else:
for l in lines:
# print('self.' + l + '(cv, 0)')
# self.init()
eval('self.' + l + '(cv, 0)')
cv.update()
'''
def mark(self, lines):
for i in range(5):
if i:
time.sleep(cpu.time)
if i % 2:
for l in lines:
cpu.ALUio[l] = True
self.show()
# print('self.' + l + '(cv, 1)')
# eval('self.' + l + '(cv, 1)')
else:
for l in lines:
cpu.ALUio[l] = False
self.show()
# print('self.' + l + '(cv, 0)')
# eval('self.' + l + '(cv, 0)')
cv.update()
'''
def init(self, cv: Canvas, root):
self.no(cv)
R_text = []
for i in range(4):
R_text.append(cpu.R(i))
R1 = tk.Label(root, text=R_text[0], fg=BLACK, font=('微软雅黑', 12), bg=WHITE)
R1.place(x=365, y=85, height=30, width=150)
R2 = tk.Label(root, text=R_text[1], fg=BLACK, font=('微软雅黑', 12), bg=WHITE)
R2.place(x=365, y=175, height=30, width=150)
R3 = tk.Label(root, text=R_text[2], fg=BLACK, font=('微软雅黑', 12), bg=WHITE)
R3.place(x=365, y=265, height=30, width=150)
R4 = tk.Label(root, text=R_text[3], fg=BLACK, font=('微软雅黑', 12), bg=WHITE)
R4.place(x=365, y=355, height=30, width=150)
# time.sleep(cpu.time)
# R1['text'] = 'RRRRRRRRRRRRRR1'
cv.update()
def ALU_R1(self, cv: Canvas, flag):
if flag:
cv.create_line(170, 273, 170, 315, width=5, capstyle=ROUND, fill=RED)
cv.create_line(270, 315, 170, 315, width=5, capstyle=ROUND, fill=RED)
cv.create_line(270, 315, 270, 100, width=5, capstyle=ROUND, fill=RED)
cv.create_line(330, 100, 270, 100, width=5, capstyle=ROUND, arrow=FIRST, fill=RED)
else:
cv.create_line(170, 273, 170, 315, width=5, capstyle=ROUND, fill=NORED)
cv.create_line(270, 315, 170, 315, width=5, capstyle=ROUND, fill=NORED)
cv.create_line(270, 315, 270, 100, width=5, capstyle=ROUND, fill=NORED)
cv.create_line(330, 100, 270, 100, width=5, capstyle=ROUND, arrow=FIRST, fill=NORED)
cv.update()
def ALU_R2(self, cv: Canvas, flag):
if flag:
cv.create_line(170, 273, 170, 315, width=5, capstyle=ROUND, fill=RED)
cv.create_line(270, 315, 170, 315, width=5, capstyle=ROUND, fill=RED)
cv.create_line(270, 315, 270, 180, width=5, capstyle=ROUND, fill=RED)
cv.create_line(330, 180, 270, 180, width=5, capstyle=ROUND, arrow=FIRST, fill=RED)
else:
cv.create_line(170, 273, 170, 315, width=5, capstyle=ROUND, fill=NORED)
cv.create_line(270, 315, 170, 315, width=5, capstyle=ROUND, fill=NORED)
cv.create_line(270, 315, 270, 180, width=5, capstyle=ROUND, fill=NORED)
cv.create_line(330, 180, 270, 180, width=5, capstyle=ROUND, arrow=FIRST, fill=NORED)
cv.update()
def ALU_R3(self, cv: Canvas, flag):
if flag:
cv.create_line(170, 273, 170, 315, width=5, capstyle=ROUND, fill=RED)
cv.create_line(270, 315, 170, 315, width=5, capstyle=ROUND, fill=RED)
cv.create_line(270, 315, 270, 275, width=5, capstyle=ROUND, fill=RED)
cv.create_line(330, 275, 270, 275, width=5, capstyle=ROUND, arrow=FIRST, fill=RED)
else:
cv.create_line(170, 273, 170, 315, width=5, capstyle=ROUND, fill=NORED)
cv.create_line(270, 315, 170, 315, width=5, capstyle=ROUND, fill=NORED)
cv.create_line(270, 315, 270, 275, width=5, capstyle=ROUND, fill=NORED)
cv.create_line(330, 275, 270, 275, width=5, capstyle=ROUND, arrow=FIRST, fill=NORED)
cv.update()
def ALU_R4(self, cv: Canvas, flag):
if flag:
cv.create_line(170, 273, 170, 315, width=5, capstyle=ROUND, fill=RED)
cv.create_line(270, 315, 170, 315, width=5, capstyle=ROUND, fill=RED)
cv.create_line(270, 315, 270, 355, width=5, capstyle=ROUND, fill=RED)
cv.create_line(330, 355, 270, 355, width=5, capstyle=ROUND, arrow=FIRST, fill=RED)
else:
cv.create_line(170, 273, 170, 315, width=5, capstyle=ROUND, fill=NORED)
cv.create_line(270, 315, 170, 315, width=5, capstyle=ROUND, fill=NORED)
cv.create_line(270, 315, 270, 355, width=5, capstyle=ROUND, fill=NORED)
cv.create_line(330, 355, 270, 355, width=5, capstyle=ROUND, arrow=FIRST, fill=NORED)
cv.update()
def R1_ALU1(self, cv: Canvas, flag):
if flag:
cv.create_line(340, 80, 340, 65, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(310, 65, 340, 65, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(310, 65, 100, 65, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(100, 190, 100, 65, width=5, capstyle=ROUND, arrow=FIRST, fill=ORANGE)
else:
cv.create_line(340, 80, 340, 65, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(310, 65, 340, 65, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(310, 65, 100, 65, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(100, 190, 100, 65, width=5, capstyle=ROUND, arrow=FIRST, fill=NOORANGE)
cv.update()
def R2_ALU1(self, cv: Canvas, flag):
if flag:
cv.create_line(340, 165, 340, 155, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(310, 155, 340, 155, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(310, 155, 310, 65, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(310, 65, 100, 65, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(100, 190, 100, 65, width=5, capstyle=ROUND, arrow=FIRST, fill=ORANGE)
else:
cv.create_line(340, 165, 340, 155, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(310, 155, 340, 155, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(310, 155, 310, 65, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(310, 65, 100, 65, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(100, 190, 100, 65, width=5, capstyle=ROUND, arrow=FIRST, fill=NOORANGE)
cv.update()
def R3_ALU1(self, cv: Canvas, flag):
if flag:
cv.create_line(340, 245, 340, 235, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(310, 235, 340, 235, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(310, 235, 310, 65, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(310, 65, 100, 65, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(100, 190, 100, 65, width=5, capstyle=ROUND, arrow=FIRST, fill=ORANGE)
else:
cv.create_line(340, 245, 340, 235, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(310, 235, 340, 235, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(310, 235, 310, 65, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(310, 65, 100, 65, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(100, 190, 100, 65, width=5, capstyle=ROUND, arrow=FIRST, fill=NOORANGE)
cv.update()
def R4_ALU1(self, cv: Canvas, flag):
if flag:
cv.create_line(340, 335, 340, 325, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(310, 325, 340, 325, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(310, 325, 310, 65, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(310, 65, 100, 65, width=5, capstyle=ROUND, fill=ORANGE)
cv.create_line(100, 190, 100, 65, width=5, capstyle=ROUND, arrow=FIRST, fill=ORANGE)
else:
cv.create_line(340, 335, 340, 325, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(310, 325, 340, 325, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(310, 325, 310, 65, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(310, 65, 100, 65, width=5, capstyle=ROUND, fill=NOORANGE)
cv.create_line(100, 190, 100, 65, width=5, capstyle=ROUND, arrow=FIRST, fill=NOORANGE)
cv.update()
def R1_ALU2(self, cv: Canvas, flag):
if flag:
cv.create_line(340, 110, 340, 130, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(295, 130, 340, 130, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(295, 130, 295, 240, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(250, 240, 295, 240, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(250, 240, 250, 160, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(230, 160, 250, 160, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(230, 160, 230, 190, width=5, capstyle=ROUND, arrow=LAST, fill=GREEN)
else:
cv.create_line(340, 110, 340, 130, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(295, 130, 340, 130, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(295, 130, 295, 240, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(250, 240, 295, 240, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(250, 240, 250, 160, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(230, 160, 250, 160, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(230, 160, 230, 190, width=5, capstyle=ROUND, arrow=LAST, fill=NOGREEN)
cv.update()
def R2_ALU2(self, cv: Canvas, flag):
if flag:
cv.create_line(340, 200, 340, 220, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(295, 220, 340, 220, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(295, 220, 295, 240, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(250, 240, 295, 240, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(250, 240, 250, 160, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(230, 160, 250, 160, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(230, 160, 230, 190, width=5, capstyle=ROUND, arrow=LAST, fill=GREEN)
else:
cv.create_line(340, 200, 340, 220, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(295, 220, 340, 220, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(295, 220, 295, 240, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(250, 240, 295, 240, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(250, 240, 250, 160, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(230, 160, 250, 160, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(230, 160, 230, 190, width=5, capstyle=ROUND, arrow=LAST, fill=NOGREEN)
cv.update()
def R3_ALU2(self, cv: Canvas, flag):
if flag:
cv.create_line(340, 290, 340, 310, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(295, 310, 340, 310, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(295, 310, 295, 240, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(250, 240, 295, 240, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(250, 240, 250, 160, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(230, 160, 250, 160, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(230, 160, 230, 190, width=5, capstyle=ROUND, arrow=LAST, fill=GREEN)
else:
cv.create_line(340, 290, 340, 310, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(295, 310, 340, 310, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(295, 310, 295, 240, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(250, 240, 295, 240, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(250, 240, 250, 160, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(230, 160, 250, 160, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(230, 160, 230, 190, width=5, capstyle=ROUND, arrow=LAST, fill=NOGREEN)
cv.update()
def R4_ALU2(self, cv: Canvas, flag):
if flag:
cv.create_line(340, 380, 340, 395, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(295, 395, 340, 395, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(295, 395, 295, 240, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(250, 240, 295, 240, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(250, 240, 250, 160, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(230, 160, 250, 160, width=5, capstyle=ROUND, fill=GREEN)
cv.create_line(230, 160, 230, 190, width=5, capstyle=ROUND, arrow=LAST, fill=GREEN)
else:
cv.create_line(340, 380, 340, 395, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(295, 395, 340, 395, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(295, 395, 295, 240, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(250, 240, 295, 240, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(250, 240, 250, 160, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(230, 160, 250, 160, width=5, capstyle=ROUND, fill=NOGREEN)
cv.create_line(230, 160, 230, 190, width=5, capstyle=ROUND, arrow=LAST, fill=NOGREEN)
cv.update()
def opa(self, cv: Canvas):
#加号
bx2, by2, bw2, bh2 = 115, 245, 100, 20
self.no(cv)
cv.create_text(bx2, by2 - 5, text='+', font=('微软雅黑', 16), anchor=NW, fill=GOLD)
cv.update()
def opb(self,cv:Canvas):
#减号
bx2, by2, bw2, bh2 = 115, 245, 100, 20
self.no(cv)
cv.create_text(bx2, by2 - 5, text='+ -', font=('微软雅黑', 16), anchor=NW, fill=GOLD)
cv.create_text(bx2, by2 - 5, text='+', font=('微软雅黑', 16), anchor=NW, fill=GREY)
cv.update()
def opc(self,cv:Canvas):
#乘号
bx2, by2, bw2, bh2 = 115, 245, 100, 20
self.no(cv)
cv.create_text(bx2, by2 - 5, text='+ - x', font=('微软雅黑', 16), anchor=NW, fill=GOLD)
cv.create_text(bx2, by2 - 5, text='+ -', font=('微软雅黑', 16), anchor=NW, fill=GREY)
cv.update()
def opd(self,cv:Canvas):
#除号
bx2, by2, bw2, bh2 = 115, 245, 100, 20
self.no(cv)
cv.create_text(bx2, by2 - 5, text='+ - x ÷', font=('微软雅黑', 16), anchor=NW, fill=GOLD)
cv.create_text(bx2, by2 - 5, text='+ - x', font=('微软雅黑', 16), anchor=NW, fill=GREY)
cv.update()
def no(self, cv:Canvas):
bx2, by2, bw2, bh2 = 115, 245, 100, 20
cv.create_rectangle(bx2, by2, bx2 + 100, by2 + 20, fill=BLACK, width=0)
cv.create_text(bx2, by2 - 25, text='完 成 运 算', font=('微软雅黑', 12), anchor=NW, fill=GREY)
cv.create_text(bx2, by2 - 5, text='+ - x ÷', font=('微软雅黑', 16), anchor=NW, fill=GREY)
cv.update()
def finish(self, cv:Canvas):
bx2, by2, bw2, bh2 = 115, 245, 100, 20
cv.create_rectangle(bx2, by2, bx2 + 100, by2 + 20, fill=BLACK, width=0)
cv.create_text(bx2, by2 - 25, text='完 成 运 算', font=('微软雅黑', 12), anchor=NW, fill=GOLD)
cv.create_text(bx2, by2 - 5, text='+ - x ÷', font=('微软雅黑', 16), anchor=NW, fill=GREY)
cv.update()
if __name__ == "__main__":
al = ALU()#实例化
al.init(cv, root)
al.line_init()
cv.update()
al.mark(['R3_ALU1'])
al.show()
# al.ALU_R1(cv, 1)
# al.ALU_R2(cv, 1)
# al.ALU_R3(cv, 1)
# al.ALU_R4(cv, 1)
# al.R1_ALU1(cv, 1)
# al.R2_ALU1(cv, 1)
# al.R3_ALU1(cv, 1)
# al.R4_ALU1(cv, 1)
# al.R1_ALU2(cv, 1)
# al.R2_ALU2(cv, 1)
# al.R3_ALU2(cv, 1)
# al.R4_ALU2(cv, 1)
def left1(event):
print(event.x,event.y)
cv.bind('<Button-1>', left1)
# cv.create_line(485,469+4.5,485+120+12,469+4.5,width=9,fill=RED,arrow=LAST)
# cv.create_rectangle(485, 469, 485 + 120, 469 + 9, fill=RED)
root.mainloop()