How to make a play again button in python

I made the game hangman inside tkinter with a friend of mine, and we are nearly finished but we'd like to add a button that says play again and replays the game, but we can't figure out the correct way of doing it. My original idea was to create a play again button to close out the current window, open up a new one with the same basis, and play the game again, but whenever it comes to the "Easy" "Medium" and "Hard" buttons, they don't do anything. I'd prefer solutions that don't include using other modules. Thanks!

EDIT: I forgot to include the code!!! I'm dumb, sorry!

    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()

I made the game hangman inside tkinter with anycodings_tkinter a friend of mine, and we are nearly finished anycodings_tkinter but we'd like to add a button that says play anycodings_tkinter again and replays the game, but we can't anycodings_tkinter figure out the correct way of doing it. My anycodings_tkinter original idea was to create a play again anycodings_tkinter button to close out the current window, open anycodings_tkinter up a new one with the same basis, and play anycodings_tkinter the game again, but whenever it comes to the anycodings_tkinter "Easy" "Medium" and "Hard" buttons, they anycodings_tkinter don't do anything. I'd prefer solutions that anycodings_tkinter don't include using other modules. Thanks!

EDIT: I forgot to include the code!!! I'm anycodings_tkinter dumb, sorry!

    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()

24

Answers 1 : of Play Again Button Tkinter Python 3

No, don't try to remake the window. anycodings_tkinter Remake the widgets in the window, the anycodings_tkinter same way you made them at the beginning. anycodings_tkinter You clearly know how to clear the canvas anycodings_tkinter and buttons. Do that and then redo the anycodings_tkinter initialization step. To make it neater, anycodings_tkinter make a function that you call both at anycodings_tkinter boot and at playagain.

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)

Also, what's with the double line anycodings_tkinter spacing? Holy cow that's annoying.

0

2022-09-30T04:53:40+00:00 2022-09-30T04:53:40+00:00Answer Link

mRahman

3

Answers 3 : of Play Again Button Tkinter Python 3

who are using OS module can refer my anycodings_tkinter code , see use global variable if u want anycodings_tkinter to play next song , why i posted it , i anycodings_tkinter solved it by myself , so i am happy so anycodings_tkinter who are using os module can refer it

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()

0

2022-09-30T04:53:40+00:00 2022-09-30T04:53:40+00:00Answer Link

jidam

Is there a button function in Python?

Practical Data Science using Python The Button widget is used to add buttons in a Python application. These buttons can display text or images that convey the purpose of the buttons. You can attach a function or a method to a button which is called automatically when you click the button.

How do you press a button in Python?

Steps by step Approach:.
Import required modules..
Create webdriver object..
Assign URL..
Use maximize_window() method to maximize the browser window. And then wait 10 seconds using sleep() method..
Use find_element_by_link_text() method to click button by text..

Can a button run two commands tkinter?

The Tkinter button has only one command property so that multiple commands or functions should be wrapped to one function that is bound to this command .