8-1 【Python0021】電子算盤

混沌武士丞發表於2024-05-22

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)

相關文章