周报12

tkinter电子算盘
import tkinter


def paint(event):
x1, y1 = (event.x - 1), (event.y - 1)
x2, y2 = (event.x + 1), (event.y + 1)
cv.create_oval(x1, y1, x2, y2, fill="green") #颜色

def click1(event):
r1 = ['', '2', '3', '4', '5']
r2 = ['6', '7', '8', '9', '10']
w = event.widget
name = str(w).split('.')[2]
num = name.split('n')[1]
if num in r1:
if w['bg'] == 'blue':
w['bg'] = 'white'

frame1.children['!button'+str(r1.index(num)+6)]['bg'] = 'blue'
elif num in r2:
if w['bg'] == 'blue':
w['bg'] = 'white'
# print(num)
# r1.index(num)
if r2.index(num)!=0:
frame1.children['!button'+str(r2.index(num)+1)]['bg'] = 'blue'
else:
frame1.children['!button']['bg'] = 'blue'


def click2(event):
r3 = ['11', '12', '13', '14', '15']
r4 = ['16', '17', '18', '19', '20']
r5 = ['21', '22', '23', '24', '25']
r6 = ['26', '27', '28', '29', '30']
r7 = ['31', '32', '33', '34', '35']
w = event.widget
name = str(w).split('.')[2]
num = int(name.split('n')[1])
print(num)
if (num-1)//5==2 or (num-1)//5==6:
if (num-1)//5==2:
if frame1.children['!button' + str(num)]['bg'] == 'red':
if frame1.children['!button' + str(num+5)]['bg'] == 'white':
frame1.children['!button' + str(num)]['bg'] = 'white'
frame1.children['!button' + str(num + 5)]['bg'] = 'red'
else:
if frame1.children['!button' + str(num)]['bg'] == 'red':
if frame1.children['!button' + str(num-5)]['bg'] == 'white':
frame1.children['!button' + str(num)]['bg'] = 'white'
frame1.children['!button' + str(num - 5)]['bg'] = 'red'

else:
if frame1.children['!button'+str(num)]['bg'] == 'red':
if frame1.children['!button'+str(num-5)]['bg'] == 'white':
frame1.children['!button' + str(num)]['bg'] = 'white'
frame1.children['!button' + str(num-5)]['bg'] = 'red'
elif frame1.children['!button'+str(num+5)]['bg'] == 'white':
frame1.children['!button' + str(num)]['bg'] = 'white'
frame1.children['!button' + str(num + 5)]['bg'] = 'red'


window = tkinter.Tk()
window.title("用户登录")
window.geometry('1000x500')
frame1 = tkinter.Frame(window, width=500, height=500, bg='green')
frame1.grid(column=0, row=0)
frame2 = tkinter.Frame(window, width=500, height=500, bg='yellow')
frame2.grid(column=1, row=0)


for i in range(2):
for k in range(5):
if i == 1:
b = tkinter.Button(frame1, text=str(i) + str(k), bd=4, width=12, height=3, bg='white')
b.bind("<Button-1>", click1)
b.grid(column=k, row=i)
else:
b = tkinter.Button(frame1, text=str(i) + str(k), bd=4, width=12, height=3, bg='blue')
b.bind("<Button-1>", click1)
b.grid(column=k, row=i)
for i in range(2, 7):
for k in range(5):
if i != 2:
b = tkinter.Button(frame1, text=str(i)+str(k), bd=4, width=12, height=3,bg='red')
b.bind("<Button-1>", click2)
b.grid(column=k, row=i)
else:
b = tkinter.Button(frame1, text=str(i) + str(k), bd=4, width=12, height=3, bg='white')
b.bind("<Button-1>", click2)
b.grid(column=k, row=i)

cv = tkinter.Canvas(frame2, width=500, height=500) #画布大小
cv.grid(column=0, row=0)
cv.bind("<B1-Motion>", paint)
# print(frame1.ge)
# frame1.children.get('!button9').widgetName='eqeq'
# print(frame1.children.)
# frame_left = tkinter.Frame(window, width=500, height=500)
# # tkinter.Label(frame_left,text='左侧标签1',bg='green',width=10,height=5).grid(row =0,column=0)
# # tkinter.Label(frame_left,text='左侧标签2',bg='blue',width=10,height=5).grid(row = 1 ,column =1)
# frame_left.place(relx=0.5, rely=0.2, relwidth=0.3, relheight=0.6)
# frame_left.pack(side=tkinter.LEFT)
# frame_right = tkinter.Frame(frame1)
# # tkinter.Label(frame_right,text='右侧标签1',bg='gray',width=10,height=5).grid(row = 0 ,column =1)
# # tkinter.Label(frame_right,text='右侧标签2',bg='pink',width=10,height=5).grid(row = 1 ,column =0)
# # tkinter.Label(frame_right,text='右侧标签3',bg='purple',width=10,height=5).grid(row= 1,column=1)
# frame_right.pack(side=tkinter.RIGHT)
window.mainloop()
posted @ 2022-05-22 08:51  我的未来姓栗山  阅读(31)  评论(0编辑  收藏  举报