简体   繁体   中英

Tkinter GUI: Replacing one button in a row with a label WITHOUT changing the size of the row. Even by a millimetre

I made a game where you have a 10*10 grid of buttons. Ten of those are randomly selected to be "tanks". If you click on a tank, the button is replaced with the label "Hit.", The problem I have is that once that happens. the row of that button is either a tiny amount longer or shorter than the other rows? How do I stop this?

button[hit] = Label(text="Hit!", padx=5.5)

在此处输入图像描述 在此处输入图像描述

If I set "padx" to 5.4, then it is just a bit shorter than the other rows rather than just a bit longer.

在此处输入图像描述

I've tried numbers in between with several decimal points, but it just doesn't work.

from tkinter import *
import random

root = Tk()
button = [""] * 100
row = [""] * 10
tank = [""] * 10
showHit = Label(text="Hit!")
prevHits = []

for i in range(10):
    x = 3
    y = random.randint(0,9)
    tank[i] = "(" + str(x) + "," + str(y) + ")"
print(*tank)

def getPos(pos):
    global tank
    print(pos)
    for i in tank:
        if pos == i:
            print("Hit!")
            a = int(pos[1])
            b = int(pos[3])
            print(a, b)  
            hit = a*10# + b
            for i in range(10):
                button[hit].destroy()
                hit += 1
            hit = a*10
            for i in range(10):
                if hit != (a*10) + b and not hit in prevHits:
                    string = "(" + str(a) + "," + str(i) + ")"
                    button[hit] = Button(root, text=string, command=lambda pos=string: getPos(pos))
                    button[hit].pack(in_=row[a], side=LEFT)
                else:
                    button[hit] = Label(text="Hit!", padx=5.4)
                    button[hit].pack(in_=row[a], side=LEFT)
                    prevHits.append(hit)
                print(hit)
                hit += 1



for r in range(len(row)):
    row[r] = Frame(root)
    row[r].pack()
    print(row[r])


num = len(button)
for i in range(num):
    t = i
    t %= 10
    if t == 0:
        r +=1
    if r == 10:
        r = 0
    string = "(" + str(r) + "," + str(t) + ")"
    button[i] = Button(root, text=string, command=lambda pos=string: getPos(pos))
    r = i // 10
    button[i].pack(in_=row[r], side=LEFT)

mainloop()

I solved the issue by replacing pack() with grid(), consequently removing all frames, and instead putting 10 buttons into each row. I also managed to improve my code:

        for i in range(10):
                if not hit != (a*10) + b and not hit in prevHits:
                    button[hit] = Label(text="Hit!", padx=5.4)
                    button[hit].grid(row=a, column=b, sticky = W)
                    prevHits.append(hit)
                print(hit)
                hit += 1

Now I can target a button precisely and change it, rather than having to rewrite an entire row in a specific order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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