How do make different messages pop up when different "difficulties" are choosen?

Question:

I want different outcomes when different "difficulties" are choosen and I press start.
I can’t make it work. I have tried to look for anwsers on youtube i didn’t find anything helpful.

import tkinter as tk
from tkinter import messagebox
import random

root = tk.Tk()

root.title("Simple Game")
canvas1 = tk.Canvas(root, width=300, height=300, bg="black")
canvas1.pack()
canvas1.create_text(150, 50, text="Simple Game", font="Times", fill="white")
root.resizable(False, False)

exitbutton = tk.Button(root, text="Exit Game", command=root.destroy, fg="black", activebackground="red").place(x=96,
                                                                                                               y=250)


def startgame():
    if DifficultyList == "Difficulty":
        messagebox.showinfo("Disclaimer", "You need to choose a difficulty!")
    elif DifficultyList == "Easy":
        messagebox.showinfo("Info", "You are now playing on Easy mode!")


startbutton = tk.Button(root, text="Start", command=startgame, fg="black", activeforeground="white",
                        activebackground="green").place(x=96, y=150)

canvas1.create_window(150, 150, window=exitbutton)
canvas1.create_window(150, 150, window=startbutton)

DifficultyList = (
    "Difficulty",
    "Easy",
    "Medium",
    "Hard"
)

variable = tk.StringVar(root)
variable.set(DifficultyList[0])

Difficulty = tk.OptionMenu(root, variable, *DifficultyList).place(x=94, y=100)

root.mainloop()
Asked By: Shutp

||

Answers:

This is part of basics of tkinter, you need to get the value from the variable/radiobutton, using get(), and check it with the value on the options list(DifficultyList), so your function would be like:

def startgame():
    if variable.get() == DifficultyList[1]: # Check if 2nd item on list is selected
        messagebox.showinfo("Info", "You are now playing on Easy mode!")
    elif variable.get() == DifficultyList[2]:
        messagebox.showinfo("Info", "You are now playing on Medium mode!")
    elif variable.get() == DifficultyList[3]:
        messagebox.showinfo("Info", "You are now playing on Hard mode!")
    else: # If the selection is anything else other than above mentioned, then
        messagebox.showinfo("Disclaimer", "You need to choose a difficulty!")

Notice how I changed your if and moved it to else, because it is the only statement left to choose from the list, hence it is part of else. Yes, you can choose any other one statement to be else too, but this is the only odd one out, so I chose this.


Note that while naming variables to follow a convention, like a list would be usually named in all small letters, but class would be named following PascalCase and function might be named using camelCase but usual variables are all small letters. Also check: Some common naming conventions are for python

Answered By: Delrius Euphoria
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.