CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
jackfrued

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: jackfrued/Python-100-Days
Path: blob/master/Day01-15/code/Day10/gui2.py
Views: 729
1
"""
2
使用tkinter创建GUI
3
- 使用画布绘图
4
- 处理鼠标事件
5
6
Version: 0.1
7
Author: 骆昊
8
Date: 2018-03-14
9
"""
10
11
import tkinter
12
13
14
def mouse_evt_handler(evt=None):
15
row = round((evt.y - 20) / 40)
16
col = round((evt.x - 20) / 40)
17
pos_x = 40 * col
18
pos_y = 40 * row
19
canvas.create_oval(pos_x, pos_y, 40 + pos_x, 40 + pos_y, fill='black')
20
21
22
top = tkinter.Tk()
23
# 设置窗口尺寸
24
top.geometry('620x620')
25
# 设置窗口标题
26
top.title('五子棋')
27
# 设置窗口大小不可改变
28
top.resizable(False, False)
29
# 设置窗口置顶
30
top.wm_attributes('-topmost', 1)
31
canvas = tkinter.Canvas(top, width=600, height=600, bd=0, highlightthickness=0)
32
canvas.bind('<Button-1>', mouse_evt_handler)
33
canvas.create_rectangle(0, 0, 600, 600, fill='yellow', outline='white')
34
for index in range(15):
35
canvas.create_line(20, 20 + 40 * index, 580, 20 + 40 * index, fill='black')
36
canvas.create_line(20 + 40 * index, 20, 20 + 40 * index, 580, fill='black')
37
canvas.create_rectangle(15, 15, 585, 585, outline='black', width=4)
38
canvas.pack()
39
tkinter.mainloop()
40
41
# 请思考如何用面向对象的编程思想对上面的代码进行封装
42
43