如何在我的tkinter GUI路径查找器中获得列之间的等间距填充?

huangapple go评论68阅读模式
英文:

How to get equal padding in between columns for my tkinter GUI pathfinder?

问题

以下是您提供的代码的翻译部分:

import tkinter as tk
import random

class PathFinder:
    def __init__(self, parent):
        self.parent = parent
        self.parent.title("PathFinder game")
        self.grid_design()
        self.reset_button()

    def grid_design(self):
        self.labels = [[tk.Label(self.parent, text=str(random.randint(1, 10)), width=6, height=3, relief='solid') for _ in range(5)] for _ in range(5)]
        self.labels[4][0].config(text="S")
        self.labels[0][4].config(text="G")

        for row in range(5):
            for column in range(5):
                self.labels[row][column].grid(row=row, column=column, padx=0, pady=0)

    def reset_button(self):
        self.reset_button = tk.Button(self.parent, text='创建新的随机场地', command=self.reset_grid)
        self.reset_button.grid(row=5, column=2, pady=10)

    def reset_grid(self):
        for row in range(5):
            for col in range(5):
                if not (row == 4 and col == 0) and not (row == 0 and col == 4):
                    new_value = str(random.randint(1, 10))
                    self.labels[row][col].config(text=new_value)

root = tk.Tk()
app = PathFinder(root)
root.mainloop()

请注意,我已经翻译了代码中的注释和字符串,并进行了一些格式调整以确保其在中文环境中能够正常运行。如果您需要进一步的帮助,请告诉我。

英文:

I am currently creating a GUI with tkinter in python for a Pathfinder app. My code up until now has a grid of 5 by 5 with an S (starting point) and G (goal/ end point) and all cells in between are random numbers. I also have a reset button that re-generates the numbers randomly, creating a different field every time. The problem now is that I do not get the output I want. I want the cells of the grid to be next to each other, seperated only by lines (_). I already tried to set the padding to 0, but that did not help. The output now gives correct padding between the first and second column, but not between the second and third column. It seems as if the third column has a way larger width. Does anyone know how i can get the correct output of a grid that has equal width and height columns, not being seperated?

Here is the code I used:

import tkinter as tk
import random


class PathFinder:
    def init(self, parent):
        self.parent = parent
        self.parent.title("PathFinder game")
        self.grid_design()self.reset_button()

    def grid_design(self):
        self.labels = [[tk.Label(self.parent, text = str(random.randint(1, 10)), width=6, height=3, relief='solid') for _ in range(5)] for _ in range(5)]
        self.labels[4][0].config( text = "S")           
        self.labels[0][4].config( text = "G")           

        for row in range(5):
            for column in range(5):
                self.labels[row][column].grid(row=row, column=column, padx=0, pady=0)

    def reset_button(self):
        self.reset_button = tk.Button(self.parent, text='Create new random field', command=self.reset_grid)
        self.reset_button.grid(row=5, column=2, pady=10)
    
    def reset_grid(self):
        for row in range(5):
            for col in range(5):
                if not (row == 4 and col == 0) and not (row == 0 and col == 4):
                    new_value = str(random.randint(1, 10))
                    self.labels[row][col].config(text=new_value)


root = tk.Tk()
app = PathFinder(root)
root.mainloop()

答案1

得分: 1

以下是翻译好的部分:

这是因为您将一个长按钮放在第2列。 请改为将其放在第0列,使用columnspan=5

还请注意,def init(self, parent) 应该改为 def __init__(self, parent)

class PathFinder:
    def __init__(self, parent):   # 应该是 __init__ 而不是 init
        ...

    ...
 
    def reset_button(self):
        self.reset_button = tk.Button(self.parent, text='创建新的随机字段', command=self.reset_grid)
        self.reset_button.grid(row=5, column=0, columnspan=5, pady=10)

    ...

结果:

如何在我的tkinter GUI路径查找器中获得列之间的等间距填充?

英文:

It is because you put a long button at column 2. Put it in column 0 with columnspan=5 instead:

Note also that def init(self, parent) should be def __init__(self, parent) instead.

class PathFinder:
    def __init__(self, parent):   # should be __init__ instead
        ...

    ...
 
    def reset_button(self):
        self.reset_button = tk.Button(self.parent, text='Create new random field', command=self.reset_grid)
        self.reset_button.grid(row=5, column=0, columnspan=5, pady=10)

    ...

Result:

如何在我的tkinter GUI路径查找器中获得列之间的等间距填充?

huangapple
  • 本文由 发表于 2023年4月17日 20:22:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76035119.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定