したがって、私のプログラムでは、grid_remove()
関数を配置するたびに、通常はウィジェットが削除されます。プログラムを初めて実行するときはいつでも (つまり、間違った推測でポイントを失うことはありません)、ほとんどのウィジェットが削除されます。ただし、推測が間違っていてポイントが取り除かれると (正常に動作します)、ウィジェットは遅延してウィンドウに残ります。何か助けはありますか?
def numright
コードは次のとおりです:(エラーはとの後のどこかで発生すると思いますdef wordguess
)
import random
from tkinter import *
from tkinter import messagebox
text_doc = open("test.txt", "r")
text = text_doc.read()
numbOfletters = len(text)
random_letter = random.randint(0,numbOfletters-1)
letter = text[random_letter]
points = 50
username = ""
is_there = 0
class GUIFramework(Frame):
def __init__(self,master=None):
Frame.__init__(self,master)
self.master.title("Window")
self.master.geometry("420x75")
self.grid(padx=10,pady=10)
self.CreateWidgets()
def CreateWidgets(self):
self.lbText = Label(self, text="Would you like to play \"Find the secret letter?\"", font="Verdana")
self.lbText.grid(row=0, column=0, columnspan = 3)
self.button = Button(self, text="Yes, please", command=self.next, background = "green")
self.button.grid(row=1, column=0)
self.btnDisplay = Button(self, text="No, thanks", command = self.window, background = "red")
self.btnDisplay.grid(row=1, column=1)
def window(self):
self.tl = Toplevel(self)
self.master.destroy()
def next(self):
self.lbText.grid_remove()
self.button.grid_remove()
self.btnDisplay.grid_remove()
self.lbText = Label(self, text="You're currently playing \"Find the secret letter\"", font="Verdana")
self.lbText.grid(row=0, column=0, columnspan = 10)
self.username = Label(self, text="Enter your username")
self.username.grid(row=1, column=0)
self.enText = Entry(self)
self.enText.grid(row=1, column=2, columnspan=5)
self.ok = Button(self, text="OK", command = self.wordguess)
self.ok.grid(row=1, column=8)
def wordguess(self):
global username
self.master.geometry("420x90")
username = self.enText.get()
self.username.grid_remove()
self.enText.grid_remove()
self.word = Label(self, text="You have {0} points, enter a word?".format(50))
self.word.grid(row=1, column=0)
self.entry = Entry(self)
self.entry.grid(row=1, column=1, columnspan=4)
self.ok = Button(self, text="OK", command = self.numright)
self.ok.grid(row=1, column=8)
def numright(self):
global points
global is_there
word = self.entry.get()
word = word.lower()
is_there = word.count(letter)
self.lbText.grid_remove()
self.word.grid_remove()
self.entry.grid_remove()
self.enText.grid_remove()
self.ok.grid_remove()
if is_there > 4:
self.master.geometry("200x70")
self.done = Label(self, text = "Congradulations, you have won!")
self.done.grid(row = 0, column = 0)
self.ok = Button(self, text="OK", command = self.window)
self.ok.grid(row=1, column=0)
else:
if (is_there < 5) and (is_there > 0):
points = points - 5
else:
points = points - 10
self.lbText = Label(self, text="You're currently playing \"Find the secret letter\"", font="Verdana")
self.lbText.grid(row=0, column=0, columnspan = 10)
self.numright = Label(self, text="That word has {} secret letters".format(is_there))
self.numright.grid(row=2, column=0)
self.word = Label(self, text="You have {0} points, enter a word?".format(points))
self.word.grid(row=1, column=0)
self.entry = Entry(self)
self.entry.grid(row=1, column=1, columnspan=4)
def scores(self):
output = open("names.txt", "a")
output.write("{0:2} {1} \n".format(str(points), username))
output.close()
output = open("names.txt", "r")
if __name__ == "__main__":
guiFrame = GUIFramework()
guiFrame.mainloop()