如何在 GUI python 中创建主菜单

时间:2020-12-20 19:52:24

标签: python python-3.x tkinter

我对编码非常陌生,我想制作一个石头剪刀布游戏。我已经使我的代码适合游戏,但我想改进它。我想制作一个启动菜单,用户在其中输入名称,然后单击按钮使游戏开始。

我尝试使用 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()

1 个答案:

答案 0 :(得分:0)

这取决于你想要什么样的菜单。要询问姓名,您可以使用输入消息框。

name = msgbox.askquestion("Name", "Enter name")

按钮菜单窗口

你可以有一个带有按钮的窗口,每次点击都会调用不同的函数。像这样:

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)

每个按钮点击调用不同的函数(来自bottom of page

文件菜单

这是记事本等应用程序的默认设置。如果你想要一个文件菜单,你可以这样做:

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()