tkinter 上的重置按钮
Reset button on tkinter
我已经从我的 CS 教学中为 A-Level 准备安排了一些有趣的工作,因为我们没有任何 'official' 工作要做,因为我已经完成了 11 年级,任务是 'Mastermind' 类型游戏
玩家按顺序猜颜色,猜对了多少
这是我第一次使用 Tkinter,我找到了 destory() 函数,但我无法重新启动程序无需再次手动按下 "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()
也是为了让您知道按钮按下计数器无法正常工作,但我无法修复它,直到我可以重置 window。
您可以使用.withdraw()
函数。
.destroy()
字面意义上的破坏了widget,一旦破坏就再也找不回来了。如果你在销毁它之后再引用它,你会得到一个错误。
所以,你需要用.withdraw()
让它暂时消失,要让它回来,你会用.deiconify()
。
这是一个例子:
from tkinter import *
window = Tk()
window2 = Toplevel()
def go_away():
window.withdraw()
def come_back():
window.deiconify()
b = Button(window, text = "go away", command = go_away).pack()
b2 = Button(window2, text = "come back", command = come_back).pack()
window.mainloop()
希望这个回答对您有所帮助!
不,没有名为 reset
的函数可以执行您 want.You 需要手动创建的功能。
在您的代码中,reset
表示:
- 生成一个新变量
answer
- 删除canvas上的所有对象。
- 初始化您设置的变量。
所以所有的代码可以是:
from tkinter import *
import random
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 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():
def closeWin():
MainCanvas.delete("all")
nonlocal NumberOfPresses,answer,coodinates
NumberOfPresses = 0
coodinates = [15, 500, 65, 550]
answer = MakeAnswer()
print(answer)
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()
我已经从我的 CS 教学中为 A-Level 准备安排了一些有趣的工作,因为我们没有任何 'official' 工作要做,因为我已经完成了 11 年级,任务是 'Mastermind' 类型游戏
玩家按顺序猜颜色,猜对了多少
这是我第一次使用 Tkinter,我找到了 destory() 函数,但我无法重新启动程序无需再次手动按下 "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()
也是为了让您知道按钮按下计数器无法正常工作,但我无法修复它,直到我可以重置 window。
您可以使用.withdraw()
函数。
.destroy()
字面意义上的破坏了widget,一旦破坏就再也找不回来了。如果你在销毁它之后再引用它,你会得到一个错误。
所以,你需要用.withdraw()
让它暂时消失,要让它回来,你会用.deiconify()
。
这是一个例子:
from tkinter import *
window = Tk()
window2 = Toplevel()
def go_away():
window.withdraw()
def come_back():
window.deiconify()
b = Button(window, text = "go away", command = go_away).pack()
b2 = Button(window2, text = "come back", command = come_back).pack()
window.mainloop()
希望这个回答对您有所帮助!
不,没有名为 reset
的函数可以执行您 want.You 需要手动创建的功能。
在您的代码中,reset
表示:
- 生成一个新变量
answer
- 删除canvas上的所有对象。
- 初始化您设置的变量。
所以所有的代码可以是:
from tkinter import *
import random
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 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():
def closeWin():
MainCanvas.delete("all")
nonlocal NumberOfPresses,answer,coodinates
NumberOfPresses = 0
coodinates = [15, 500, 65, 550]
answer = MakeAnswer()
print(answer)
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()