当我放入按钮时,我的 Tkinter 标签消失了

My Tkinter label dissapeared when i put in the buttons

我正在尝试使用带有 python 的 tkinter 创建一个简单的刽子手游戏。在创建 GUI 时,我首先输入的是破折号。lbl = tkinter.Label(window, text= xhash)。当我放入纽扣时,标签消失了。我已经尝试将 window 变大,但它仍然没有恢复。

这是我的完整代码:

#importing modules
import tkinter
import random

#creating the dashes 
f = open(r'C:\Users\Gareth\Documents\hangman\words.txt', 'r') 
word = f.readlines()
guess = random.choice(word)
hash = []
xhash = ""
while len(hash) < len(str(guess)):
    hash.append('_ ')
def rejoin():
    xhash = "".join(hash)
    xhash.strip("[]")
    xhash.strip(",")
    xhash.strip("'")

#creating the window
window = tkinter.Tk()
window.minsize(300, 300)

#commands for buttons a-z
def acmd():
    for i in list(guess):
        if i == 'a':
            hash[guess.index(i)] = 'a'
            rejoin()
            lbl.configure(text= xhash)
    abtn.configure(bg = 'red')

#label
lbl = tkinter.Label(window, text= xhash)

#buttons for letters a-z
abtn = tkinter.Button(window, text = 'a', command = acmd)
bbtn = tkinter.Button(window, text = 'b')
cbtn = tkinter.Button(window, text = 'c')
dbtn = tkinter.Button(window, text = 'd')...

#placing lbl and buttons
lbl.grid(row = 1, column = 1)
abtn.grid(row = 3 , column = 1)
bbtn.grid(row = 3 , column = 2)
cbtn.grid(row = 3 , column = 3)...
window.mainloop()

您需要正确放置它们,很可能是您没有放置它们或将它们放在彼此之上。您需要使用 grid() 来定位它们

例子-

lbl = tkinter.Label(window, text= xhash)
lbl.grid(row=0,column=1)
abtn = tkinter.Button(window, text = 'a', command = acmd)
abtn.grid(row=0,column=2)

问题不在标签内,而在文本内。

def rejoin(): xhash = "".join(hash) xhash.strip("[]") xhash.strip(",") xhash.strip("'") 唯一需要的调整是一份全球声明…… 否则,函数中所做的任何事情都与其他任何事情无关,因此,之前分配的 xhash = '',只是留空...