我和我的一个朋友在tkinter里面制作了游戏刽子手,我们差不多完成了但是我们想添加一个按钮再次播放并重放游戏,但是我们无法找出正确的方法。正在做。我最初的想法是创建一个再次播放按钮来关闭当前窗口,打开一个具有相同基础的新窗口,并再次玩游戏,但无论何时谈到“简单”“中”和“硬”按钮,他们什么都不做。我更喜欢不包括使用其他模块的解决方案。谢谢!
编辑:我忘了包含代码!!!我很笨,抱歉! from tkinter import *
import random as r
import time as tm
#Hang Man
class menu:
def __init__(self):
self.game = Tk()
self.game.geometry('600x600+50+50')
self.game.title("Hang Man")
self.canvas = Canvas(self.game,height=400,width=800,bg='light goldenrod yellow')
self.canvas.delete(ALL)
self.canvas.create_line(100,50,100,500,tags="Line") #Vertical
self.canvas.create_line(20,500,80,500,tags="Line") #Lower Horizontal
self.canvas.create_line(100,50,150,50,tags="Line") #Horizontal Line
self.canvas.pack()
self.play = Button(self.game,text="Play",command=self.playbt)
self.play.pack(side=BOTTOM)
self.game.mainloop() # Create an event loop
def playagain(self):
self.game.destroy()
self.__init__()
def playbt(self):
self.difs()
def difs(self):
self.play.destroy()
self.easy = Button(self.game,text="Easy",command=self.easy)
self.medium = Button(self.game,text="Medium",command=self.medium)
self.hard = Button(self.game,text="Hard",command=self.hard)
self.easy.pack(side=BOTTOM)
self.medium.pack(side=BOTTOM)
self.hard.pack(side=BOTTOM)
def easy(self):
ewords = r.choice(["TABLE","CHAIR","DESK","PHONE","LIGHT","MAN"])
self.playP(ewords.lower())
def medium(self):
mwords = r.choice(["PYTHON","LAPTOP","JACKET","VIDEO","MODULE","LIBRARY"])
self.playP(mwords.lower())
def hard(self):
hwords = r.choice(["PROGRAM","TOLEDO","UNIVERSITY","ENGINEER","FOOTBALL","LANGUAGE"])
self.playP(hwords.lower())
def playP(self,words):
self.lwords = words
for i in range(0, len(self.lwords)):
self.canvas.create_text(300+20*i,310,text="_",font="Times 16",tags="text")
self.hm = 0
self.easy.destroy()
self.medium.destroy()
self.hard.destroy()
self.myscore = int(0)
self.te = StringVar() #Text Entry TextVariable
self.teb = Entry(self.game, textvariable = self.te) #Text Entry Box
self.tebt = Button(self.game,text="Submit", command = self.checkAnswer)
self.teb.pack(side=BOTTOM)
self.tebt.pack(side=BOTTOM)
def checkAnswer(self):
temp= self.te.get()
score=0 #trial set for each try
score1=0
x1=300
for i in range(0,len(self.lwords)):
if temp==self.lwords[i]:
self.canvas.create_text(x1+20*i,300,text=self.lwords[i],font="Times 16",tags="text")
score1+=1
self.myscore += 1
if self.myscore == len(self.lwords):
self.win()
if not (score1 > score):
self.draw()
score=0
score1
def win(self):
self.canvas.delete(ALL)
self.canvas.after(100)
self.teb.destroy()
self.tebt.destroy()
self.canvas.create_text(400,200,text = 'YOU WIN!!',font='Times 36')
self.pa = Button(self.game,text="Play Again?",command=self.playagain)
self.pa.pack(side=BOTTOM)
def draw(self):
self.hm += 1
if self.hm == 1:
self.canvas.create_oval(125,50,175,100, tags = "Line") #Head
elif self.hm == 2:
self.canvas.create_line(150,100,150,150, tags = "Line") #Body
elif self.hm == 3:
self.canvas.create_line(150,125,125,100, tags = "Line") #Left Arm
elif self.hm == 4:
self.canvas.create_line(150, 125, 175, 100, tags = "Line") #Right Arm
elif self.hm == 5:
self.canvas.create_line(150,150,125,175, tags = "Line") #Left Leg
elif self.hm == 6:
self.canvas.create_line(150,150,175,175, tags = "Line") #Right Leg
self.canvas.update()
self.canvas.after(100)
self.canvas.delete(ALL)
self.canvas.create_text(200,200,text="HANG MAN!!",font="Times 36")
self.tebt.destroy()
self.teb.destroy()
self.pa = Button(self.game,text="Play Again?",command=self.playagain)
self.pa.pack(side=BOTTOM)
menu()
答案 0 :(得分:1)
不,不要试图重拍窗户。在窗口中重新制作小部件,就像在开始时制作小部件一样。你清楚地知道如何清除画布和按钮。执行此操作然后重做初始化步骤。为了使它更整洁,创建一个你在启动和播放时调用的功能。
def __init__(self):
self.game = Tk()
self.game.geometry('600x600+50+50')
self.game.title("Hang Man")
self.canvas = Canvas(self.game,height=400,width=800,bg='light goldenrod yellow')
self.canvas.delete(ALL)
self.canvas.pack()
self.init_ui()
self.game.mainloop() # Create an event loop
def playagain(self):
self.canvas.delete('ALL') # clear the canvas
self.init_ui()
def init_ui(self):
self.canvas.create_line(100,50,100,500,tags="Line") #Vertical
self.canvas.create_line(20,500,80,500,tags="Line") #Lower Horizontal
self.canvas.create_line(100,50,150,50,tags="Line") #Horizontal Line
self.play = Button(self.game,text="Play",command=self.playbt)
self.play.pack(side=BOTTOM)
另外,双线间距是多少?神圣的牛,这很烦人。
答案 1 :(得分:0)
我同意Novel,你应该重新创建窗口而不是Tkinter实例,但我想提供一个与你原始想法一致的简单解决方案。
最简单的答案是改变
此:
def playagain(self):
self.game.destroy()
self.__init__()
为:
def playagain(self):
self.game.destroy()
menu()
这将结束当前的Tkinter实例并通过调用"菜单"来启动一个新实例。再次上课。
答案 2 :(得分:0)
正在使用OS模块的人可以引用我的代码,如果您想播放下一首歌曲,请参阅使用全局变量,为什么要发布它,我自己解决了,所以我很高兴 所以使用os模块的人可以参考它
from tkinter import *
import os
root = Tk()
root.geometry("500x500")
root.title("music player ")
Label(root, text="MUSIC PLAYER").pack()
i=0
def prev():
pass
def paly():
music_dir = 'D:\songs'
songs = os.listdir(music_dir)
print(songs)
os.startfile(os.path.join(music_dir, songs[0]))
def nxt():
global i
music_dir = 'D:\songs'
songs = os.listdir(music_dir)
print(songs)
os.startfile(os.path.join(music_dir, songs[i]))
i=i+1
f1=Frame(root).pack()
Button(f1,text="prev",command=prev).pack(side=LEFT,padx=30)
Button(f1,text="play/pause",command=paly).pack(side=LEFT,padx=30)
Button(f1,text="next",command=nxt).pack(side=LEFT,padx=30)
root.mainloop()