编程语言
首页 > 编程语言> > [Python3]利用tkinter为数独计算器编写GUI

[Python3]利用tkinter为数独计算器编写GUI

作者:互联网

这两天学习了一下tkinter,当中遇到的困难还是很多的,不过最终还是做出来了。

import tkinter as tk
import os
def calculate():
    before = number1.get('1.0', 'end')
    fo = open('sudoku_data.txt','w')
    fo.write(before)
    fo.close()
    f = open('sudoku_result.txt')
    os.system('sudoku.py')
    s = f.read()
    f.close()
    txt2.delete('1.0', 'end')
    txt2.insert('1.0', s)

root = tk.Tk()
root.title('数独计算器  author: zqy')
root.geometry('370x650')
tip1 = tk.Label(root, text = '输入:', font = ('微软雅黑', 12))
tip1.grid(row = 1, column = 0)
txt = tk.Label(root, text = '输入想要计算的数独,以0来代替空', font = ('微软雅黑', 12))
txt.grid(row = 0, column = 1)
number1 = tk.Text(root, width = 9, height = 9,font = ('微软雅黑', 12), bd = 1)
number1.grid(row = 1, column = 1)
number1.insert('1.0', '0'*81)
start = tk.Button(root, width = 4, text = '计算', command = calculate)
start.grid(row = 2, column = 1)
tip2 = tk.Label(root, text = '输出:', font = ('微软雅黑', 12))
tip2.grid(row = 3, column = 0)
txt2 = tk.Text(root, width = 9, height = 9, font = ('微软雅黑', 12), bd = 1)
txt2.grid(row = 3, column = 1)

col_count, row_count = root.grid_size()
for col in range(col_count):
    root.grid_columnconfigure(col, minsize=20)

for row in range(row_count):
    root.grid_rowconfigure(row, minsize=100)

root.resizable(0, 0)
root.mainloop()

结合的上一篇代码:

import sys
fo = open("sudoku_data.txt")
sudoku = [0 for i in range(9)]
for i in range(0,9):
    sudoku[i] = list(map(int, fo.readline().replace('\n', '')))
fo.close()
def canfill(i, j, k, sudoku): 
    '''当前行 列 填入的数字 数独'''
    for m in range(0, 9):
        if (sudoku[i][m] == k or sudoku[m][j] == k or sudoku[i // 3 * 3 + m // 3][j // 3 * 3 + m % 3] == k):
            return False
    return True
def solve(i, j, sudoku):
    if (i == 9):
        result = open("sudoku_result.txt",'w')
        for k in range(0, 9):
            for m in range(0, 9):
                result.write(str(sudoku[k][m]))
            result.write('\n')
        result.close()
        sys.exit()
    if (sudoku[i][j] != 0):
        solve(i if j < 8 else i + 1, (j + 1) % 9, sudoku)   
    for k in range(1, 10):
        if (canfill(i, j, k, sudoku)):
            sudoku[i][j] = k
            solve(i if j < 8 else i + 1, (j + 1) % 9, sudoku)     
            sudoku[i][j] = 0		#如果下一个数填写失败就让当前所填成功的数为0以避开上一个if的条件,进行下一个k的尝试
solve(0, 0, sudoku)

效果

岛风酱
文件夹

思路

通过两个文件来连接两个py文件,曾尝试用import来连接两个py文件但是失败了,希望以后可以找到办法解决,这样就不用用文件这么麻烦了。
关于控件的布局,通过遍历行列来设置间距,之前试着用pack布局但是没学会(?)

col_count, row_count = root.grid_size()
for col in range(col_count):
    root.grid_columnconfigure(col, minsize=20)

for row in range(row_count):
    root.grid_rowconfigure(row, minsize=100)

这三行分别设置窗口标题、窗口大小以及窗口大小不可变(因为放大或者缩小会影响布局)。

root.title('数独计算器  author: zqy')
root.geometry('370x650')
root.resizable(0, 0)

调用os库来运行另一个py文件,在其往data文件中写入后再读取,插入到下方的文本框里。

os.system('sudoku.py')

另外,之前的列表初始化比较蠢,学了一个写起来轻松的方法。

sudoku = [0 for i in range(9)]

标签:sudoku,tkinter,GUI,range,grid,tk,root,Python3,row
来源: https://blog.csdn.net/m0_51439562/article/details/116545404