简体   繁体   中英

Tkinter Minesweeper

I am creating a minesweeper game in Tkinter. The entire game is finished with the bombs and clicking; however I do not know how to make a "You Win" in the game. It doesn't have to be exact, but I want a way for the user to be able to win the game. I was thinking of "if buttons(not the bomb) are forget(), then print a messagebox saying "You Win". Any help is greatly appreciated

from tkinter import *#imports tkinter
from tkinter import messagebox#imports the module messagebox from tkinter
import pygame #imports the module Pygame 
sounds = pygame.mixer #allows the import of sounds a.k.a .wav files
sounds.init()#init() is called when an object is created from a class


app=Tk()#creates the app
app.geometry('250x230')#the exact size/grid of the app
app.resizable(0,0)#0,0 allows a "lock" on the app


def destroy1():# a function that destroys buttons so user cannot click on them once they click once
    b1h.place_forget()#forget() is a function that makes the script 'forget' the button existed

def destroy2():# a function that destroys buttons so user cannot click on them once they click once
    bf7.place_forget()#forget() is a function that makes the script 'forget' the button existed

def destroy3():
    b11v.place_forget()#forget() is a function that makes the script 'forget' the button existed

def destroy4():
    bf13t.place_forget()#forget() is a function that makes the script 'forget' the button existed

    
def destroy5():
    ba112.place_forget()#forget() is a function that makes the script 'forget' the button existed
    ba11.place_forget()#forget() is a function that makes the script 'forget' the button existed
    ba1.place_forget()#forget() is a function that makes the script 'forget' the button existed
    b2.place_forget()#forget() is a function that makes the script 'forget' the button existed
    ba.place_forget()#forget() is a function that makes the script 'forget' the button existed
    bf7.place_forget()#forget() is a function that makes the script 'forget' the button existed

def destroy6():
    bf1io.place_forget()#forget() is a function that makes the script 'forget' the button existed

def destroy7():
    b287.place_forget()#forget() is a function that makes the script 'forget' the button existed

def destroy71():
    b111356.place_forget()#forget() is a function that makes the script 'forget' the button existed

def destroy72():
    b11124.place_forget()#forget() is a function that makes the script 'forget' the button existed
    
def destroy73():
    bf13245.place_forget()#forget() is a function that makes the script 'forget' the button existed

def destroy74():
    b211245.place_forget()#forget() is a function that makes the script 'forget' the button existed

def destroy75():
    b21134.place_forget()#forget() is a function that makes the script 'forget' the button existed

def increase():#a function to increase font size
    fontsize= fontStyle['size']#makes a variable call fontsize
    labelExample['text'] = fontsize + 2#the label of the text is equal to the fontsize + 2
    fontStyle.configure(size=fontsize+2)#configure() is the function that allows the configuration of the fontsize

def win():
    s = sounds.Sound("Sound Effect (3).wav")#plays a selected .wav file from the folder
    s.play()#play() is the function that plays the wav.file in the tkinter
    messagebox.showinfo("Lose", "You Lose!")#displays a messagebox 
    app.destroy()#closes the tkinter window

def bomb():
    s = sounds.Sound("C.wav")#plays a selected .wav file from the folder
    s.play()#play() is the function that plays the wav.file in the tkinter
    messagebox.showinfo("Lose", "You Lose!")#displays a messagebox 
    app.destroy()#closes the tkinter window

def instruct():#A function
    s = sounds.Sound("07_button7.wav")#plays a selected .wav file from the folder
    s.play()#play() is the function that plays the wav.file in the tkinter
    messagebox.showinfo("Instruction",  "The numbers on the board represent how many bombs are adjacent to a square. For example, if a square has a 2 on it, then there are 2 bombs next to that square. The bombs could be above, below, right left, or diagonal to the square. Avoid all the bombs  to win Minesweeper.")    

Label(app, text = "Mini Minesweeper", font=("Courier",15)).pack()#
Label(app, text = "There are two bombs", font=("Courier",10)).pack()#


#row 1
b787 = Button(app, text = 'Help\n🙋', width = 6, height=2 , command=instruct) # a button that displays an answer in a text, and when pressed it activates incorrect()
b787.place (relx=0.01, rely=0.85, anchor =W)#rely and relx controls the format where the button is placed


b1 = Button(app, text = '💣', width = 6 ) # a button that displays an answer in a text, and when pressed it activates incorrect()
b1.place (relx=0.10, rely=0.30, anchor =W)#rely and relx controls the format where the button is placed
b1h = Button(app, text = '', width = 6, command=lambda : [destroy1(), bomb()] ) # a button that displays an answer in a text, and when pressed it activates incorrect()
b1h.place (relx=0.10, rely=0.30, anchor =W)#rely and relx controls the format where the button is placed



bf = Button(app, text = '1' , width = 6) # a button that displays an answer in a text, and when pressed it activates incorrect()
bf.place (relx=0.30 , rely=0.30, anchor =W)#rely and relx controls the format where the button is placed
bf7 = Button(app, text = '', width = 6, command=destroy2) # a button that displays an answer in a text, and when pressed it activates incorrect()
bf7.place (relx=0.30 , rely=0.30, anchor =W)#rely and relx controls the format where the button is placed



b2 = Button(app, text = '', width = 6, command=destroy5)# a button that displays an answer in a text, and when pressed it activates correct()
b2.place(relx=0.50 , rely=0.30, anchor =W)#rely and relx controls the format where the button is placed



ba = Button(app, text = '', width = 6, command=destroy5)# a button that displays an answer in a text, and when pressed it activates correct()
ba.place(relx=0.70, rely=0.30, anchor =W)#rely and relx controls the format where the button is placed

#row 2
b11 = Button(app, text = '2', width = 6 ) # a button that displays an answer in a text, and when pressed it activates incorrect()
b11.place (relx=0.10, rely=0.40, anchor =W)#rely and relx controls the format where the button is placed
b11v = Button(app, text = '', width = 6, command= destroy3 ) # a button that displays an answer in a text, and when pressed it activates incorrect()
b11v.place (relx=0.10, rely=0.40, anchor =W)#rely and relx controls the format where the button is placed



bf1 = Button(app, text = '2', width = 6) # a button that displays an answer in a text, and when pressed it activates incorrect()
bf1.place (relx=0.30 , rely=0.40, anchor =W)#rely and relx controls the format where the button is placed
bf1io = Button(app, text = '', width = 6, command=destroy6) # a button that displays an answer in a text, and when pressed it activates incorrect()
bf1io.place (relx=0.30 , rely=0.40, anchor =W)#rely and relx controls the format where the button is placed



b21 = Button(app, text = '1', width = 6)# a button that displays an answer in a text, and when pressed it activates correct()
b21.place(relx=0.50 , rely=0.40, anchor =W)#rely and relx controls the format where the button is placed
b287 = Button(app, text = '', width = 6, command= destroy7)# a button that displays an answer in a text, and when pressed it activates correct()
b287.place(relx=0.50 , rely=0.40, anchor =W)#rely and relx controls the format where the button is placed



ba1 = Button(app, text = '', width = 6, command=destroy5)# a button that displays an answer in a text, and when pressed it activates correct()
ba1.place(relx=0.70, rely=0.40, anchor =W)#rely and relx controls the format where the button is placed

#row3

b111 = Button(app, text = '1', width = 6 ) # a button that displays an answer in a text, and when pressed it activates incorrect()
b111.place (relx=0.10, rely=0.50, anchor =W)#rely and relx controls the format where the button is placed
b111356 = Button(app, text = '', width = 6, command=destroy71 ) # a button that displays an answer in a text, and when pressed it activates incorrect()
b111356.place (relx=0.10, rely=0.50, anchor =W)#rely and relx controls the format where the button is placed


bf13 = Button(app, text = '💣', width =6) # a button that displays an answer in a text, and when pressed it activates incorrect()
bf13.place (relx=0.30 , rely=0.50, anchor =W)#rely and relx controls the format where the button is placed
bf13t = Button(app, text = '', width =6, command=lambda : [destroy4(), bomb()] ) # a button that displays an answer in a text, and when pressed it activates incorrect()
bf13t.place (relx=0.30 , rely=0.50, anchor =W)#rely and relx controls the format where the button is placed




b211 = Button(app, text = '1', width = 6)# a button that displays an answer in a text, and when pressed it activates correct()
b211.place(relx=0.50 , rely=0.50, anchor =W)#rely and relx controls the format where the button is placed
b21134 = Button(app, text = '', width = 6, command=destroy75)# a button that displays an answer in a text, and when pressed it activates correct()
b21134.place(relx=0.50 , rely=0.50, anchor =W)#rely and relx controls the format where the button is placed


ba11 = Button(app, text = '', width = 6, command=destroy5)# a button that displays an answer in a text, and when pressed it activates correct()
ba11.place(relx=0.70, rely=0.50, anchor =W)#rely and relx controls the format where the button is placed

#row 4

b1112 = Button(app, text = '1', width = 6 ) # a button that displays an answer in a text, and when pressed it activates incorrect()
b1112.place (relx=0.10, rely=0.60, anchor =W)#rely and relx controls the format where the button is placed
b11124 = Button(app, text = '', width = 6, command= destroy72 ) # a button that displays an answer in a text, and when pressed it activates incorrect()
b11124.place (relx=0.10, rely=0.60, anchor =W)#rely and relx controls the format where the button is placed



bf132 = Button(app, text = '1', width = 6) # a button that displays an answer in a text, and when pressed it activates incorrect()
bf132.place (relx=0.30 , rely=0.60, anchor =W)#rely and relx controls the format where the button is placed
bf13245 = Button(app, text = '', width = 6, command= destroy73) # a button that displays an answer in a text, and when pressed it activates incorrect()
bf13245.place (relx=0.30 , rely=0.60, anchor =W)#rely and relx controls the format where the button is placed

b2112 = Button(app, text = '1', width = 6)# a button that displays an answer in a text, and when pressed it activates correct()
b2112.place(relx=0.50 , rely=0.60, anchor =W)#rely and relx controls the format where the button is placed
b211245 = Button(app, text = '', width = 6, command= destroy74)# a button that displays an answer in a text, and when pressed it activates correct()
b211245.place(relx=0.50 , rely=0.60, anchor =W)#rely and relx controls the format where the button is placed

ba112 = Button(app, text = '', width =6, command=destroy5)# a button that displays an answer in a text, and when pressed it activates correct()
ba112.place(relx=0.70, rely=0.60, anchor =W)#rely and relx controls the format where the button is placed

You can show the message using tkinter.messagebox

This will look more appealing and you dont have to reframe your existing buttons

tkinter.messagebox.showinfo(title="Result", message="You Won,!!", **options)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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