【发布时间】:2014-04-04 20:35:56
【问题描述】:
我有 .txt 文件,其中包含我自己输入的单词,但该程序确实允许您输入自己的单词。问题似乎是,当我单击一个按钮时,它不会将其识别为对单词的猜测,我意识到这段代码还远未完成,但任何帮助将不胜感激如果您还可以让我知道如何在键盘上方输入字段,那就太好了:D
from tkinter import*
import time
import random
root= Tk()
top = Tk()
top.title('Hangmanz')
top.geometry('500x140')
root.geometry('200x250')
root.title('Photo')
def appear(index, letter):
# This line would be where you insert the letter in the textbox
guess = ('')
guess += str(letter)
if letter in line:
print(char,end=''),
# set the players guess to guesses
w.insert(0,letter)
# Disable the button by index
buttons[index].config(state="disabled")
letters=["Q", "A", "Z", "W", "S", "X", "E", "D", "C", "R", "F", "V", "T", "G", "B",
"Y", "H", "N", "U", "J", "M", "I", "K", "O", "L", "P"]
# A collection (list) to hold the references to the buttons created below
buttons = []
for index in range(26):
n=letters[index]
button = Button(top, bg="White", text=n, width=5, height=1, relief=GROOVE,
command=lambda index=index, n=n: appear(index, n))
# Add the button to the window
button.grid(padx=2, pady=2, row=index%3, column=index//3)
# Add a reference to the button to 'buttons'
buttons.append(button)
w = Entry(root,width=22, fg="black",
bg="#f2f2f2", font="Impact")
w.pack()
#photo
canvas = Canvas(root,width = 200, height = 200, bg = 'white')
canvas.pack(expand = YES, fill = BOTH)
gif1 = PhotoImage(file = 'Scaffold01.gif')
canvas.create_image(50, 10, image = gif1, anchor = NW)
ok=True
while ok==True:
answer = input ("Would you like to input a new word? Enter Y or N ")
if answer == ('Y'):
file = open("Hangmanwords.txt", "a")
d =input("enter new word ")
file.write("\n"+d)
file.close()
c=input("would you like to add another word? Y or N")
if c == ('y'):
ok=True
elif c == ('N'):
ok=False
elif answer == ('N'):
ok=False
time.sleep(1)
print("Get ready to play!")
line = random.choice(open("Hangmanwords.txt","r").readlines())
print(line)
for char in line:
print("*")
root.mainloop()
top.mainloop()
【问题讨论】:
-
这段代码有太多基本问题:你不应该使用
input和Tkinter,你不应该在GUI中调用time.sleep,你不应该有一个除了事件循环之外的无限循环,你不应该调用mainloop两次。