Python Forum
How can I add reset button?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I add reset button?
#2
Migrate your code to a class, it eliminates the use of globals.
One way to clear a number of buttons is assign them to a frame. Them you can
pack_forget() and clear it all. Make more functions, I modified your script into 2 functions,
the other part could be all in your class init function.
here's one way to reset your game
import random
import time
from tkinter import Tk, Button, DISABLED, messagebox, Frame
from functools import partial
 
 
def close_window():
    root.destroy()
 
def show_symbol(x, y):
    global first
    global previousX, previousY
    global moves
    global pairs
    
    buttons[x, y]['text'] = button_symbols[x, y]
    buttons[x, y].update_idletasks()
    if first:
        previousX = x
        previousY = y
        first = False
        moves = moves + 1
    elif previousX != x or previousY != y:
        if buttons[previousX, previousY]['text'] != buttons[x, y]['text']:
            time.sleep(0.5)
            buttons[previousX, previousY]['text'] = ''
            buttons[x, y]['text'] = ''
        else:
            buttons[previousX, previousY]['command'] = DISABLED
            buttons[x, y]['command'] = DISABLED
            pairs = pairs + 1
            if pairs == len(buttons) / 2:
                messagebox.showinfo('Matching', 'Number of moves: ' + str(moves))
                frame.pack_forget()
                frame.pack(expand='yes',fill='both')
                make_board()
        first = True
 
 
def make_board():
    global button_symbols
    my_font= ('arial',30,'bold')
    button_symbols = {}
    symbols = [u'\u2702', u'\u2702', u'\u2705', u'\u2705', u'\u2708', u'\u2708',
               u'\u2709', u'\u2709', u'\u270A', u'\u270A', u'\u270B', u'\u270B',
               u'\u270C', u'\u270C', u'\u270F', u'\u270F', u'\u2712', u'\u2712',
               u'\u2714', u'\u2714', u'\u2716', u'\u2716', u'\u2728', u'\u2728',
              ]
 
    random.shuffle(symbols)
 
 
    for x in range(6):
        for y in range(4):
            button = Button(frame,command=partial(show_symbol, x, y),
                            width=5, height=3, border=2,font= my_font)
            button.grid(column=x, row=y,padx=15,pady=20)
            buttons[x, y] = button
            button_symbols[x, y] = symbols.pop()
 
 
root = Tk()
root.title('Igra Memorije')
root.resizable(width=False, height=False)

frame= Frame(root)
frame.pack(expand='yes',fill='both')
 
buttons = {}
first = True
previousX = 0
previousY = 0
moves = 0
pairs = 0

make_board()

 

 
 
root.mainloop()
Reply


Messages In This Thread
How can I add reset button? - by andrex353 - Jun-06-2022, 06:12 PM
RE: How can I add reset button? - by joe_momma - Jun-10-2022, 02:39 AM
RE: How can I add reset button? - by deanhystad - Jun-12-2022, 04:41 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  How can I reset my Label? JJota 5 12,013 Mar-11-2020, 04:40 PM
Last Post: JJota
  Reset Button did not work ardvci 2 3,695 Mar-02-2020, 07:59 PM
Last Post: ardvci
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 6,170 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] Reset Button CPD3408 3 22,936 Jan-25-2019, 11:55 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020