当我按下

时间:2015-08-14 04:53:55

标签: python tkinter python-3.4

我正在尝试使用tkinter和python创建一个简单的刽子手游戏。在创建GUI时,我放入的第一件事是破折号。lbl = tkinter.Label(window, text= xhash)。当我放入按钮时,标签消失了。我试过让窗户更大,但它仍然没有回来。

这是我的完整代码:

#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()

2 个答案:

答案 0 :(得分:0)

您需要正确定位它们,最有可能是您没有定位它们或将它们定位在彼此之上。您需要使用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)

答案 1 :(得分:0)

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

def rejoin(): xhash = "".join(hash) xhash.strip("[]") xhash.strip(",") xhash.strip("'") 唯一需要的调整是全球声明...... 没有它,无论在函数中做了什么与其他任何东西都无关,所以,先前分配的xhash ='',只是留空......