简体   繁体   English

使用 PySimpleGUI 重叠帧

[英]Overlapping Frames with PySimpleGUI

I want to create a GUI using pySimpleGUI that resembles the Killer Sudoku game.我想使用类似于 Killer Sudoku 游戏的 pySimpleGUI 创建一个 GUI。 For those unfamiliar with Killer Sudoku, it is similar to regular sudoku however there is an extra level of complexity where not only do you have the 9 3x3 boxes where the number 1-9 has to be used exactly one time per frame, column and row but there are also cages.对于那些不熟悉 Killer Sudoku 的人来说,它类似于常规数独,但是有一个额外的复杂程度,你不仅有 9 个 3x3 的盒子,其中数字 1-9 必须每帧、每列和每行只使用一次但也有笼子。 These cages have a number that specifies what all the numbers in the cage must sum up to.这些笼子有一个数字,指定笼子中所有数字的总和。 No number can be repeated in the cage either.笼子里也不能重复任何数字。 Here is an example of what one of these boards looks like.这是其中一个板的外观示例。

Killer Sudoku Board杀手数独板

As you can see, not only would I need a frame to outline each 3x3, but also a frame for each cage on the board.如您所见,我不仅需要一个框架来勾勒出每个 3x3 的轮廓,而且还需要一个框架来为板上的每个笼子提供框架。 The issue comes when a cage is in both a column and a row (an L shape).当笼子同时位于一列和一排(L 形)时,问题就出现了。 Does pySimpleGUI have the ability to frame abnormal shapes like what would be required for this sudoku game? pySimpleGUI 是否能够像这个数独游戏所需要的那样对异常形状进行构图?

Thanks for any assistance感谢您的帮助

Show how to draw the board on sg.Graph in following example script, of course, not everything drawn here在下面的示例脚本中展示如何在sg.Graph上绘制棋盘,当然,这里没有绘制所有内容

There're still lot of works to do to implement your Killer Sudoku game.要实现您的 Killer Sudoku 游戏,还有很多工作要做。

import PySimpleGUI as sg

class Sudoku():

    def __init__(self):
        # Use monospaced font here
        self.columns = 9
        self.blocks = 3
        self.bg = 'green'
        self.line_width = (1, 3, 6)
        self.line_color = ("black", "black", 'grey')
        self.number_color = ("white", "yellow")
        self.font0 = ("Courier New", 20, 'bold')
        self.font1 = ("Courier New", 12)
        self.w0, self.h0 = self.char_size(self.font0)
        self.w1, self.h1 = self.char_size(self.font1)
        self.pad = 8
        w = self.w0 + 2 * (self.w1 + 2 * self.pad)
        h = self.h0 + 2 * self.pad
        self.cell_width = max(w, h)
        self.width = (self.cell_width * self.columns + 2 * self.line_width[2])
        self.size = (self.width, self.width)
        self.numbers0, self.numbers1 = [], []

    def char_size(self, font):
        root = sg.tk.Tk()
        size = (sg.tk.font.Font(font=font).measure("W"),
            sg.tk.font.Font(font=font).metrics('linespace'))
        root.destroy()
        return size

    def draw_board1(self, element):
        delta = self.columns * self.cell_width
        for i in range(self.columns+1):
            x = self.line_width[2] + i * self.cell_width
            x0, x1 = self.line_width[2], self.line_width[2] + delta
            y = self.line_width[2] + i * self.cell_width
            y0, y1 = self.line_width[2], self.line_width[2] + delta
            element.draw_line((x0, y), (x1, y), color=self.line_color[0],
                width=self.line_width[0])
            element.draw_line((x, y0), (x, y1), color=self.line_color[0],
                width=self.line_width[0])

    def draw_board2(self, element):
        delta = self.columns * self.cell_width
        for i in range(self.blocks+1):
            x = self.line_width[2] + 3 * i * self.cell_width
            x0, x1 = self.line_width[2], self.line_width[2] + delta
            y = self.line_width[2] + 3 * i * self.cell_width
            y0, y1 = self.line_width[2], self.line_width[2] + delta
            element.draw_line((x0, y), (x1, y), color=self.line_color[1],
                width=self.line_width[1])
            element.draw_line((x, y0), (x, y1), color=self.line_color[1],
                width=self.line_width[1])

    def draw_number0(self, element, data):
        if self.numbers0:
            element.Widget.delete(*self.numbers)
        self.numbers0 = []
        offset_x = self.line_width[2] + self.cell_width//2
        offset_y = self.cell_width*self.columns + self.line_width[2] - self.cell_width//2
        for row in range(self.columns):
            for col in range(self.columns):
                item = element.draw_text(str(data[row][col]),
                    (offset_x + self.cell_width * col, offset_y - self.cell_width * row),
                    font=self.font0, color=self.number_color[0])
                self.numbers0.append(item)

    def draw_number1(self, element, data):
        if self.numbers1:
            element.Widget.delete(*self.numbers)
        self.numbers1 = []
        offset_x = self.line_width[2] + self.pad + self.w1
        offset_y = self.cell_width*self.columns + self.line_width[2] - self.pad - self.h1//2
        for row, col, number in data:
            item = element.draw_text(str(number),
                (offset_x + self.cell_width * col, offset_y - self.cell_width * row), font=self.font1,
                color=self.number_color[1])
            self.numbers1.append(item)

s = Sudoku()
data0 = list(map(list, [
    "739541286", "168923457", "245687139", "973862541", "512374968",
    "684195372", "421738695", "397256814", "856419723"]))
data1 = [
    (0, 0, 11), (0, 2, 17), (0, 3, 9), (0, 5, 3), (0, 7, 19),
    (1, 1, 15), (1, 3, 11), (1, 5, 10), (1,6, 10), (1,8, 16),
    (2, 0, 16), (2, 3, 14), (2, 4, 16), (2, 7, 8),
    (3, 1, 8), (3, 2, 5),
    (4, 3, 14), (4, 6, 12), (4, 7, 13), (4, 8, 15),
    (5, 0, 16), (5, 2, 12), (5, 3, 13), (5, 5, 13),
    (6, 0, 7), (6, 3, 9), (6, 6, 16),
    (7, 1, 22), (7, 4, 11), (7, 6, 15), (7, 8, 9),
    (8, 2, 10), (8, 4, 10),
]

layout = [
    [sg.Graph(s.size, (0, 0), s.size, background_color=s.bg, key='GRAPH',
        enable_events=True)],
]

window = sg.Window("Killer Sudoku", layout, margins=(0, 0), finalize=True)
graph = window['GRAPH']
s.draw_board1(graph)
s.draw_board2(graph)
s.draw_number0(graph, data0)
s.draw_number1(graph, data1)

while True:
    event, values = window.read()
    if event in (sg.WINDOW_CLOSED, 'Exit'):
        break
    print(event, values)
window.close()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM