Python Forum
Adding the timer, smiley face, and flags in Minesweeper.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding the timer, smiley face, and flags in Minesweeper.
#4
Images in buttons is pretty easy. Just create an image for the flag, then when you flag a cell set the text to '' and set the image. I clipped this from a tk Yahtzee game.
import pathlib
import tkinter as tk
import random

IMAGE_DIR = pathlib.Path(__file__).parent

class DiceButton(tk.Button):
    """Roll a die"""
    def __init__(self, parent):
        # Create dice images
        self.images = [tk.PhotoImage(file=IMAGE_DIR/f'dice{i}.png') for i in range(7)]
        super().__init__(parent, image=self.images[0], command=self.roll)
        self.value = 0

    def roll(self):
        self.value = random.randint(1, 6)
        self['image'] = self.images[self.value]

ROOT = tk.Tk()
for _ in range(6):
    DiceButton(ROOT).pack(side=tk.LEFT)
ROOT.mainloop()
Reply


Messages In This Thread
RE: Adding the timer, smiley face, and flags in Minesweeper. - by deanhystad - May-03-2021, 08:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding timer on the Messagebox aniyanetworks 6 11,731 Feb-13-2019, 07:48 PM
Last Post: aniyanetworks
  Python minesweeper game gtk3 get label value after click lukassz 0 3,278 Mar-25-2017, 08:14 PM
Last Post: lukassz

Forum Jump:

User Panel Messages

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