更新代码

master
bettleChen 11 months ago
parent f706bb341d
commit 319d4da764

87
UI.py

@ -7,6 +7,7 @@
# -*- encoding: utf-8 -*-
import tkinter as tk
from pprint import pprint
from tkinter import *
from data import *
import time
@ -26,7 +27,7 @@ GREY = '#808080'
GREYLESS = '#CCCCCC'
BC = 40
side = 40
# 构建主窗口
width, height = 840, 610#设置长宽
@ -35,16 +36,17 @@ root.title("数独")#设置窗口标题
root.config(bg=BLACK)#设置背景
root.geometry(f'{width}x{height}')#设置窗口大小
root.resizable(0, 0) # 防止用户调整尺寸
cv = tk.Canvas(root, bg=WHITE, bd=2, relief="sunken", width=width, height=height, background=ORANGE,
borderwidth=0, highlightthickness=0)#设置画布
cv.place(x=0, y=0, anchor=NW)#设置画布位置
canvas = tk.Canvas(root, bg=WHITE, bd=2, width=width, height=height, background=ORANGE,
highlightthickness=0)#设置画布
bc = 40
canvas.place(x=0, y=0, anchor=NW)#设置画布位置
side = 40
"""文字框"""
top_x = (bc + 3) * 11 # 文本框顶点x
top_x = (side + 3) * 11 # 文本框顶点x
top_y = 50 # 文本框顶点y
bc_x = (bc + 3) * 7 + 20 # 文本框宽度
bc_y = (bc + 3) * 12 # 文本框高度
bc_x = (side + 3) * 7 + 20 # 文本框宽度
bc_y = (side + 3) * 12 # 文本框高度
right = tk.Frame(root, bg=WHITE, relief="sunken", width=bc_x, height=bc_y)
right.place(x=top_x, y=top_y, anchor=NW) # 右边栏
@ -93,10 +95,10 @@ class MyTimer:
self.min.set(now // 60)
self.sec.set(now - self.min.get() * 60)
def rect_draw(bc, row, col):
def rect_draw(side, row, col):
'''
绘制一个方格
:param bc: 方格大小
:param side: 方格边长
:param row:
:param col:
:return:
@ -108,9 +110,32 @@ def rect_draw(bc, row, col):
color = "#FAE067"
else:
color = 'white'
cv.create_rectangle(row * (bc + 3) + 50, col * (bc + 3) + 50, row * (bc + 3) + 50 + bc, col * (bc + 3) + 50 + bc,
fill=color, width=0) # 绘制一个方格
canvas.create_rectangle(row * (side + 3) + 50, col * (side + 3) + 50, row * (side + 3) + 50 + side, col * (side + 3) + 50 + side,
fill=color, width=0) # 绘制一个方格
def draw_all_rect():
"""
左上角开始
:param left_x:
:param top_y:
:return:
"""
for i in range(9):
for n in range(9):
rect_draw(side, i, n)
martix_position_mapping = dict()
def creat_martix_mapping(side):
mapping_dict = dict()
offeset_x = 50
side = side + 3
for i in range(9): # 坐标映射
for n in range(9):
x = side *i+offeset_x
y = side *n+offeset_x
mapping_dict[n, i] = [x, y]
return mapping_dict
def show_Time(timer: MyTimer):
'''
@ -125,10 +150,10 @@ def show_Time(timer: MyTimer):
Sec = tk.Label(right, textvariable=timer.sec, bg=WHITE, fg=BLACK, font=('幼圆', 16, 'bold')) # 秒钟计数
Sec.place(x=220, y=30, width=30, height=30)
def num_draw(bc, row, col, martix, u_martix):
def num_draw(side, row, col, martix, u_martix):
'''
绘制方格中的数字
:param bc: 方格大小
:param side: 方格大小
:param row:
:param col:
:param martix: 原矩阵
@ -145,10 +170,8 @@ def num_draw(bc, row, col, martix, u_martix):
else:
color = 'purple' # 用户填的数字的颜色
if martix[row, col] != 0 or u_martix[row, col] != 0: #如果该格子有数据要显示
cv.create_text(col * (bc + 3) + 50 + bc / 2, row * (bc + 3) + 50 + bc / 2,
text=f"{u_martix[row, col]}", font=('幼圆', 18, 'bold'), fill=color)
canvas.create_text(col * (side + 3) + 50 + side / 2, row * (side + 3) + 50 + side / 2,
text=f"{u_martix[row, col]}", font=('幼圆', 18, 'bold'), fill=color, tags="num")
def Difficulty(empty):
@ -191,9 +214,6 @@ def seq_recode(right, STEP):
def empty(u_martix):
count = 0 # 计数
for i in range(9):
@ -224,15 +244,30 @@ if __name__ == "__main__":
[0, 2, 8, 0, 5, 1, 0, 6, 9],
[0, 1, 3, 0, 6, 0, 7, 4, 8],
[0, 5, 0, 4, 0, 0, 0, 1, 2]])
# text_enter(BC)
# text_enter(side)
# time1.start() # 计时开始
# draw((5, 5),M,u_M)
# draw((5, 5),M,u_martix)
BC = 40
side = 40
def left1(event):
print(event.x, event.y)
cv.bind('<Button-1>', left1)
x = event.x
y = event.y
for key, posit_lt in martix_position_mapping.items():
if posit_lt[0] < x < posit_lt[0]+side and posit_lt[1] < y < posit_lt[1]+side:
print(key)
canvas.bind('<Button-1>', left1)
draw_all_rect()
martix_position_mapping = creat_martix_mapping(side)
seq_recode(right,[])
time1 = MyTimer() # 实例计时器
show_Time(time1)
emp = empty(u_M)
Difficulty(emp)
noter(True)
mainloop()

@ -0,0 +1 @@
print('hello world')

File diff suppressed because it is too large Load Diff

Binary file not shown.

@ -0,0 +1,6 @@
xrandr --addmode VNC-0 "1920x1200"
xrandr --output VNC-0 --mode "1920x1200"
nohup python3 /home/headless/Desktop/workspace/myshixun/src/new_UI.py
echo "执行开始"
echo "过程正确"
echo "执行完成"

@ -35,6 +35,7 @@ main_menu = tk.Menu(root)
root.config(menu=main_menu)
game_menu(main_menu)
auto_menu(main_menu)
martix_position_mapping = creat_martix_mapping(side)
def speed_set(spd):
global wait_time
@ -176,6 +177,8 @@ def load_game():
draw(loca, martix, u_martix, STEP)
def left1(event):
'''
martix坐标映射
@ -185,21 +188,17 @@ def left1(event):
""""""
x = event.x
y = event.y
row = -1
col = -1
global STEP, loca, loca_o
for cur_col in range(9):
for cur_row in range(9):
top_x, top_y = cur_col * (bc + 3) + 50, cur_row * (bc + 3) + 50# 计算每个方格的四个角
ud_x, ud_y = (cur_col + 1) * (bc + 3) + 50, (cur_row + 1) * (bc + 3) + 50# 计算每个方格的四个角
if x < ud_x and x > top_x:#计算点击的列数
col = cur_col
if y < ud_y and y > top_y:#计算点击的行数
row = cur_row
if row >= 0 and col >= 0 and not martix[row, col]:
for key, posit_lt in martix_position_mapping.items():
if posit_lt[0] < x < posit_lt[0] + side and posit_lt[1] < y < posit_lt[1] + side:
print(key)
if key[0] >= 0 and key[1] >= 0 and not martix[key[0], key[1]]:
# print(col, row, "dks")
if loca:#若已经点击过坐标
loca_o = loca # 保留上次点击过的坐标
loca = (row + 1, col + 1) # 更新选中的格子位置
loca = (key[0] + 1, key[1] + 1) # 更新选中的格子位置
draw(loca, martix, u_martix, STEP)#绘制界面
return
@ -267,17 +266,17 @@ def u_number(num, loca):
possible[row][col].add(num)
draw(loca, martix, u_martix, STEP)#重新绘制界面
def control_draw_old(bc):
def control_draw_old(side):
'''
绘制数字按钮
:param bc: 全局按钮大小
:param side: 全局按钮大小
:return:
'''
for i in range(9):#遍历0~9
top_x, top_y = i * (bc + 3) + 50, 12 * (bc + 3) + 10#计算每个按钮左上角坐标
top_x, top_y = i * (side + 3) + 50, 12 * (side + 3) + 10#计算每个按钮左上角坐标
B = tk.Button(root, text=f"{i + 1}", bg='white', fg='black',
font=('幼圆', 14, 'bold'), command=lambda num=i + 1: u_number(num, loca))
B.place(x=top_x, y=top_y, width=bc + 3, height=bc + 3)#绘制按钮到图上
B.place(x=top_x, y=top_y, width=side + 3, height=side + 3)#绘制按钮到图上
def control_draw(bc, u_martix):
'''
绘制数字按钮
@ -331,19 +330,19 @@ def listen_all_note(possible, u_martix):
u_martix[row][col] = 10#标记这个格子为存在提示
draw(loca, martix, u_martix, STEP)#重新绘制界面
def show_hint(bc, u_martix, possible):
def show_hint(side, u_martix, possible):
'''
显示提示信息
:param bc: 方格大小
:param side: 方格大小
:return:
'''
hint_bc = int(bc // 3)# 小数字的间距
hint_bc = int(side // 3)# 小数字的间距
for row in range(9):
for col in range(9):
if not u_martix[row][col] == 10:
possible[row][col].clear()# 只有u_martix[j][i]为10才表示该格子有笔记存在
continue# 若该格无笔记,则跳过该格
x, y = col * (bc + 3) + 50 + bc / 2, row * (bc + 3) + 50 + bc / 2# 计算大方格中点坐标
x, y = col * (side + 3) + 50 + side / 2, row * (side + 3) + 50 + side / 2# 计算大方格中点坐标
x = x - hint_bc#计算左上角第一个数字坐标
y = y - hint_bc#计算左上角第一个数字坐标
for ii in range(3):# 按照九宫格显示小数字
@ -352,9 +351,9 @@ def show_hint(bc, u_martix, possible):
if possible[row][col]: # 在用户已填的部分有可能导致部分格子无解,所以先加一个判断
for k in possible[row][col]:
if n == k:# 如果possible[row][col]中含有k则说明该格子可以填k则将k显示在方格中
cv.create_text(x + ii * hint_bc, y + jj * hint_bc,
text=f"{n}", font=('楷体', 10), fill='purple')
cv.update()#更新画布
canvas.create_text(x + ii * hint_bc, y + jj * hint_bc,
text=f"{n}", font=('楷体', 10), fill='purple', tags="mini_num")
@ -430,6 +429,15 @@ def control(bc):
font=('幼圆', 12, 'bold'), command=lambda: listen_hint(loca, u_martix))
B5.place(x=(lon + 2) * 4 + 50, y=(bc + 3) * 11 + 8, width=lon, height=bc)
# def draw(loca, martix, u_martix, STEP):
def draw_fixation_num():
for i in range(9):
for n in range(9):
x,y = martix_position_mapping[i,n]
canvas.create_text(x+ side / 2, x + side / 2,
text=f"{u_martix[i, n]}", font=('幼圆', 18, 'bold'), fill="black", tags="fixa_num")
num_draw(side,i,n,martix,u_martix)
def draw(loca, martix, u_martix, STEP):
'''
@ -437,33 +445,33 @@ def draw(loca, martix, u_martix, STEP):
:param loca: 当前位置
:return:
'''
for i in range(9):
for j in range(9):# 绘制小方格
rect_draw(BC, i, j)
if loca != ():#根据当前选中位置绘制阴影
canvas.delete("rect")
row, col = loca[0] - 1, loca[1] - 1
for i in range(9):
for j in range(9):
if col == i and row == j:#当前选中的方格使用灰色标记
cv.create_rectangle(col * (BC + 3) + 52, row * (BC + 3) + 52, col * (BC + 3) + 48 + BC,
row * (BC + 3) + 48 + BC, fill='grey', width=0)
canvas.create_rectangle(col * (side + 3) + 52, row * (side + 3) + 52, col * (side + 3) + 48 + side,
row * (side + 3) + 48 + side, fill='grey', width=0, tags="rect")
elif col == i or row == j:#当前选中方格所在行列使用浅灰色标记
cv.create_rectangle(i * (BC + 3) + 52, j * (BC + 3) + 52, i * (BC + 3) + 48 + BC,
j * (BC + 3) + 48 + BC, fill='#CCCCCC', width=0)
show_hint(BC, u_martix, possible)# 显示提示信息
for row in range(9):
for col in range(9):# 在小方格上绘制数字
num_draw(BC, row, col, martix, u_martix)
print(u_martix.tolist())
print(loca)
control(BC)# 绘制五个控制按键
control_draw(BC, u_martix)# 绘制9个数字按钮
canvas.create_rectangle(i * (side + 3) + 52, j * (side + 3) + 52, i * (side + 3) + 48 + side,
j * (side + 3) + 48 + side, fill='#CCCCCC', width=0, tags="rect")
canvas.delete("num")
num_draw(side, row, col, martix, u_martix)
canvas.delete("mini_num")
show_hint(side, u_martix, possible)# 显示提示信息
# print(u_martix.tolist())
# print(loca)
empty_count = empty(martix)# 计算数组空格
Difficulty(empty_count)# 根据空格数量显示难度
noter(note_mark)# 显示笔记提示
seq_recode(right, STEP)# 显示步骤
win(u_martix, time1, STEP)# 检查游戏是否结束
cv.update()# 更新画布
# canvas.update()# 更新画布
N = 30 # 空白格子数
martix = InitMartix(N)
@ -488,12 +496,18 @@ step = [] # 操作步骤
STEP = [[0, (0, 0), 0, martix]] # 记录每一步的状态
seq = 1 # 全局操作次序初始化
draw_all_rect() # 显示底图
control(side) # 绘制五个控制按键
control_draw(side, u_martix) # 绘制9个数字按钮
draw_fixation_num()
time1 = MyTimer() # 实例计时器
# 显示时间信息
show_Time(time1)
draw(loca, martix, u_martix, STEP)
cv.bind('<Button-1>', left1)
canvas.bind('<Button-1>', left1)
if __name__ == "__main__":
mainloop()

@ -0,0 +1,15 @@
from pycallgraph2 import PyCallGraph
from pycallgraph2.output import GraphvizOutput
from pycallgraph2 import Config
from pycallgraph2 import GlobbingFilter
def main():
# TODO: 调用各种类、函数
return
if __name__ == "__main__":
graphviz = GraphvizOutput()
graphviz.output_file = 'graph.png'
with PyCallGraph(output=graphviz):
main()

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.2 KiB

Loading…
Cancel
Save