简体   繁体   中英

How do I create a main menu in GUI python

I'm very new to coding, and I want to make a rock, paper and scissors game. I've made my code right for the game but I want to improve it. I want to make a start-up menu where the user enters the name and then clicks on a button to make the game start.

I've tried to use toplevel() but I think there's a better way of doing it. My next mission after solving that problem is to get the user info and display it in the actual game.

I would be really happy if someone could help:)

I know that the game is not complete, but the question is how do I create a main menu!

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

It depends on what kind of menu you want. To ask a name, you can use an input message box.

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

button menu window

You could have a window with buttons where each click calls a different function. Something like this:

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)

where each button click calls a different function (from bottom of page )

file menu

This is the default in apps like notepad. If you want a file menu, you can do this:

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

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