![](/img/trans.png)
[英]I am able to create a dropdown menu but I want it to be within the GUI of the main program in python
[英]How do I create a main menu in GUI python
我对编码很陌生,我想做一个石头、纸和剪刀的游戏。 我已经为游戏编写了正确的代码,但我想改进它。 我想制作一个启动菜单,用户在其中输入名称,然后单击一个按钮以启动游戏。
我试过使用 toplevel() 但我认为有更好的方法。 解决该问题后,我的下一个任务是获取用户信息并将其显示在实际游戏中。
如果有人可以提供帮助,我会很高兴:)
我知道游戏不完整,但问题是如何创建主菜单!
import tkinter
import random
root = tkinter.Tk()
root.title("Game!!")
root.geometry("400x500")
Computerchoice = random.randint(1, 3)
if Computerchoice == 1:
computerchoice = "Rock"
elif Computerchoice == 2:
computerchoice = "Paper"
elif Computerchoice == 3:
computerchoice = "Scissors"
def Rock():
label_userchoice["text"] = "Rock"
if computerchoice == "Rock":
label_result["text"] = "TIE"
label_computerchoice["text"] = "ROCK"
elif computerchoice == "Sissors":
label_result["text"] = "Computer Wins"
label_computerchoice["text"] = "PAPER"
elif computerchoice == "Sissors":
label_result["text"] = "You WIN"
label_computerchoice["text"] = "Sissors"
def Paper():
label_userchoice["text"] = "Paper"
if computerchoice == "Rock":
label_result["text"] = "You win"
label_computerchoice["text"] = "ROCK"
elif computerchoice == "Paper":
label_result["text"] = "TIE"
label_computerchoice["text"] = "Paper"
elif computerchoice == "Sissors":
label_result["text"] = "Computer Wins"
label_computerchoice["text"] = "Sissors"
def Scissors():
label_userchoice["text"] = "Scissors"
if computerchoice == "Rock":
label_result["text"] = "Computer wins"
label_computerchoice["text"] = "Rock"
elif computerchoice == "Paper":
label_result["text"] = "You win"
label_computerchoice["text"] = "Paper"
elif computerchoice == "Scissors":
label_result["text"] = "TIE"
label_computerchoice["text"] = "Scissors"
def Retry():
global computerchoice
random_datornsval = random.randint(1, 3)
if random_datornsval == 1:
computerchoice = "Rock"
elif random_datornsval == 2:
computerchoice = "Paper"
elif random_datornsval == 3:
computerchoice = "Scissors"
label_computerchoice["text"] = ""
label_userchoice["text"] = ""
label_result["text"] = "Choose!"
#Widgets
label_result = tkinter.Label(root, text="Choose")
label_result.pack()
Button_rock = tkinter.Button(root, text="Rock", command = Rock)
Button_rock.pack()
Button_scissor = tkinter.Button(root, text="Scissors", command = Scissors)
Button_scissor.pack()
Button_paper = tkinter.Button(root, text="Paper", command = Paper)
Button_paper.pack()
label_computerchoice = tkinter.Label(root, text= "")
label_computerchoice.pack()
label_userchoice = tkinter.Label(root, text="")
label_userchoice.pack()
Button_restart = tkinter.Button(root, text="Retry", command = Retry)
Button_restart.pack()
root.mainloop()
这取决于你想要什么样的菜单。 要询问姓名,您可以使用输入消息框。
name = msgbox.askquestion("Name", "Enter name")
您可以拥有一个带有按钮的 window,每次单击都会调用不同的 function。 像这样的东西:
top = tk.Tk()
top.title("MsgBox Test")
btn1 = tk.Button(top, text = "showinfo", command = btn1_clicked)
btn1.pack(fill = tk.X)
btn2 = tk.Button(top, text = "showwarning", command = btn2_clicked)
btn2.pack(fill = tk.X)
btn3 = tk.Button(top, text = "showerror", command = btn3_clicked)
btn3.pack(fill = tk.X)
每个按钮单击都会调用不同的 function (从 页面底部)
这是记事本等应用程序的默认设置。 如果你想要一个文件菜单,你可以这样做:
from tkinter import *
root = Tk()
root.title("Menu Bar")
root.geometry("640x480")
my_menu=Menu(root)
root.config(menu=my_menu)
def our_command():
my_label = Label(root, text="Clicked!!").pack()
file_menu= Menu(my_menu)
my_menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="New...",command=our_command)
file_menu.add_separator()
file_menu.add_command(label="Exit",command=root.quit)
mButton=Menubutton(root,text="Click")
mButton.grid()
edit_menu = Menu(my_menu)
my_menu.add_cascade(label="Edit",menu=edit_menu)
edit_menu.add_command(label="Cut",command=our_command)
edit_menu.add_command(label="Copy",command=our_command)
mButton.menu.add_checkbutton(label="Copy")
mButton.pack()
option_menu = Menu(my_menu)
my_menu.add_cascade(label="Edit",menu=option_menu)
option_menu.add_command(label="Find",command=our_command)
option_menu.add_command(label="Find Next",command=our_command)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.