如何在选择不同的"difficulties"时弹出不同的消息?
How do make different messages pop up when different "difficulties" are choosen?
当我选择不同的“难度”并按下开始时,我希望得到不同的结果。
我不能让它工作。我试图在 youtube 上寻找答案,但没有找到任何有用的信息。
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()
这是 tkinter
基础知识的一部分,您需要使用 get()
从 variable/radiobutton 中获取值,并将其与选项列表中的值进行核对(DifficultyList
), 所以你的函数就像:
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!")
请注意我是如何更改您的 if
并将其移至 else
,因为它是列表中唯一可供选择的语句,因此它是 else
的一部分。是的,你也可以选择任何其他语句作为 else
,但这是唯一的奇数,所以我选择了这个。
请注意,虽然命名变量要遵循约定,例如列表通常会以所有小写字母命名,但 class 将按照 PascalCase and function might be named using camelCase but usual variables are all small letters. Also check: Some common naming conventions are for python
命名
当我选择不同的“难度”并按下开始时,我希望得到不同的结果。 我不能让它工作。我试图在 youtube 上寻找答案,但没有找到任何有用的信息。
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()
这是 tkinter
基础知识的一部分,您需要使用 get()
从 variable/radiobutton 中获取值,并将其与选项列表中的值进行核对(DifficultyList
), 所以你的函数就像:
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!")
请注意我是如何更改您的 if
并将其移至 else
,因为它是列表中唯一可供选择的语句,因此它是 else
的一部分。是的,你也可以选择任何其他语句作为 else
,但这是唯一的奇数,所以我选择了这个。
请注意,虽然命名变量要遵循约定,例如列表通常会以所有小写字母命名,但 class 将按照 PascalCase and function might be named using camelCase but usual variables are all small letters. Also check: Some common naming conventions are for python
命名