繁体   English   中英

如何在 Python 上添加重置按钮

[英]How Do I add reset button on Python

["

import random
import time
from tkinter import Tk, Button, DISABLED, messagebox

def close_window(self):
    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', 'Broj poteza: ' + str(moves))
        first = True

root = Tk()
root.title('Igra Memorije')
root.resizable(width=False, height=False)

buttons = {}
first = True
previousX = 0
previousY = 0
moves = 0
pairs = 0

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(command=lambda x=x, y=y: show_symbol(x, y), width=5, height=3, border=2)
        button.grid(column=x, row=y,padx=15,pady=20)
        buttons[x, y] = button
        button_symbols[x, y] = symbols.pop()

root.mainloop()

["

import random
import time
from tkinter import Tk, Button, DISABLED, messagebox

def close_window(self):
    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', 'Broj poteza: ' + str(moves))

                for k in tuple(buttons):
                    buttons.pop(k).grid_forget()
                Button(root, text='Restart', command=start).grid(padx=15, pady=20)
        first = True

root = Tk()
root.title('Igra Memorije')
root.resizable(width=False, height=False)



buttons = {}
first = True
previousX = 0
previousY = 0
moves = 0
pairs = 0

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',
          ]

def start():
    global buttons
    global symbols
    global first
    global previousX, previousY
    global moves
    global pairs

    first = True
    previousX = 0
    previousY = 0
    moves = 0
    pairs = 0

    random.shuffle(symbols)

    for child in root.winfo_children():
        child.grid_forget()

    width, height = 6, 4

    for x in range(width):
        for y in range(height):
            button = Button(root, command=lambda x=x, y=y: show_symbol(x, y), width=5, height=3, border=2)
            button.grid(column=x, row=y,padx=15,pady=20)
            buttons[x, y] = button
            button_symbols[x, y] = symbols[x * height + y]

Button(root, text='Start', command=start).grid(padx=15,pady=20)

root.mainloop()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM