Я подготовил забавную работу из моего CS-курса для подготовки к A-Level, так как у нас нет никакой «официальной» работы, которую я выполнил, как я закончил 11-й год, задача - игра типа «Mastermind»
Игрок угадывает цвета в порядке, и сколько прав
Я впервые использую Tkinter , я нашел судьбу (), но затем я не могу перезапустить программу, не нажимая снова «run» вручную.
Есть ли способ удалить все виджеты, а затем снова отобразить их. Как кнопка сброса.
вот мой код:
#the main window
win = Tk()
#the main frame
MainFrame = Frame(win)
#the frame which contains the buttons
ButtonFrame = Frame(win)
#will contain the game
MainCanvas = Canvas(MainFrame, height=550)
#the bottom most frame, contains the new game button
BottomFrame = Frame(win)
def closeWin():
win.destroy()
def updateCoordinates(coodinates,NumberOfPresses):
for i in range (4):
print(i)
if coodinates[i] == coodinates[0]:
print(coodinates)
coodinates[i] = coodinates[i] + 60
print(coodinates)
elif coodinates[i] == coodinates[1] and NumberOfPresses >= 5:
coodinates[i] = coodinates[i] - 60
print(coodinates)
elif coodinates[i] == coodinates[2]:
coodinates[i] = coodinates[i] + 60
print(coodinates)
elif coodinates[i] == coodinates[3] and NumberOfPresses >= 5:
coodinates[i] = coodinates[i] - 60
print(coodinates)
return coodinates
#this is run is the red button is pressed
def redGuess(coodinates,NumberOfPresses):
redGuess = MainCanvas.create_oval(coodinates, fill="red")
NumberOfPresses = NumberOfPresses + 1
updateCoordinates(coodinates,NumberOfPresses)
return NumberOfPresses, coodinates
def blueGuess(coodinates,NumberOfPresses):
blueGuess = MainCanvas.create_oval(coodinates, fill="blue")
NumberOfPresses = NumberOfPresses + 1
updateCoordinates(coodinates,NumberOfPresses)
return NumberOfPresses, coodinates
def MakeAnswer():
#the list of the colours
colours = list("rbygwo")
#randomizes them
random.shuffle(colours)
#creates a new list of the randomised letters
order = ''.join(colours)
result = ''
for i in (order):
#loops through each letter and adds a space bettween them
result = result + i + ' '
#calls the new list answer AND puts each letter into a sepearte index in a list
answer = result.split()
#removes one colour from the answer
answer.pop(5)
return answer
#runs the main program
def NewGame():
NumberOfPresses = 0
coodinates = [15,500,65,550]
answer = MakeAnswer()
redButton = Button(ButtonFrame,height=3, width=7 ,bg= "red", command =lambda: redGuess(coodinates,NumberOfPresses))
blueButton = Button(ButtonFrame,height=3, width=7 ,bg= "blue", command =lambda: blueGuess(coodinates, NumberOfPresses))
yellowButton = Button(ButtonFrame,height=3, width=7 ,bg= "yellow")
greenButton = Button(ButtonFrame,height=3, width=7 ,bg= "green")
whiteButton = Button(ButtonFrame,height=3, width=7 ,bg= "white")
orangeButton = Button(ButtonFrame,height=3, width=7 ,bg= "orange")
#displays all the widgets in the window
MainFrame.pack()
ButtonFrame.pack()
MainCanvas.pack()
BottomFrame.pack(side=BOTTOM)
redButton.pack(side=LEFT, fill=X)
blueButton.pack(side=LEFT, fill=X)
yellowButton.pack(side=LEFT, fill=X)
greenButton.pack(side=LEFT, fill=X)
whiteButton.pack(side=LEFT, fill=X)
orangeButton.pack(side=LEFT, fill=X)
reset_button = Button(BottomFrame, text="Start A New Game", command = closeWin)
reset_button.pack(side=BOTTOM, fill=X)
#displayes the correct sequence
print(answer)
return
#starting coodinates of the first guess
#format= [x1, y1, x2, y2]
#Start of the game
NewGame()
#creates the buttons to select the different colours
#runs the game continuously
win.mainloop()
также просто для того, чтобы вы знали, что счетчик нажатий кнопок не работает должным образом, но я не могу это исправить, пока не смогу сбросить окно.