如何在 python 中制作开始菜单?

How can i make a startmenu in python?

我一直在尝试为我制作的游戏添加开始菜单,并且到处寻找如何制作开始菜单。我确实尝试使用 senddex 的教程,但它不适用于我的代码!我也看过许多其他地方。有谁知道如何制作开始菜单 - 谢谢!这是我的代码! (对于错误的缩进和空格之类的东西,我很抱歉,我发现很难让它在这里工作!

from tkinter import * 
import pongtablev2, redball, bat


black = (0,0,0)
white = (255,255,255)
red = (255,0,0)

x_velocity = 10
y_velocity = 0
score_left = 0
score_right = 0
score_top = 0
score_bottom = 0
first_serve = True


window = Tk()
window.title("MyPong")


my_pongtablev2 = pongtablev2.Table(window, net_colour="blue",vertical_net=True, horizontal_net=True)


my_redball = redball.Ball(table=my_pongtablev2, x_speed=x_velocity,y_speed=y_velocity,
                          width=24, height=24, colour="green", x_start=288, y_start=290)

bat_L = bat.Bat(table=my_pongtablev2, width=15, height=100, x_posn=20, y_posn=250, colour="red")
bat_R = bat.Bat(table=my_pongtablev2, width=15, height=100, x_posn=575, y_posn=250, colour="red")
bat_T = bat.Bat(table=my_pongtablev2, width=100, height=15, x_posn=250, y_posn=15, colour="red")
bat_B = bat.Bat(table=my_pongtablev2, width=100, height=15, x_posn=250, y_posn=570, colour="red")


def game_flow():
    global first_serve
    global score_left
    global score_right
    global score_top
    global score_bottom


if(first_serve == True):
    my_redball.stop_ball()
    first_serve = False

bat_L.detect_collision(my_redball)
bat_R.detect_collision(my_redball)
bat_T.detect_collision(my_redball)
bat_B.detect_collision(my_redball)


if(my_redball.x_posn <= 3):
    my_redball.stop_ball()
    my_redball.start_position()
    bat_L.start_position()
    bat_R.start_position()
    bat_T.start_position()
    bat_B.start_position
    my_pongtablev2.move_item(bat_L.rectangle, 20, 250, 35, 350)
    my_pongtablev2.move_item(bat_R.rectangle, 575, 250, 590, 350)
    my_pongtablev2.move_item(bat_T.rectangle, 250, 15, 350, 30)
    my_pongtablev2.move_item(bat_B.rectangle, 250, 570, 350, 585) 
    score_left = score_left + 1
    if(score_left >= 10):
        score_left = "WINNER!!!"
        score_right = "Looser"
        score_top = "Looser"
        score_bottom = "Looser"
    first_serve = True
    my_pongtablev2.draw_score(score_left, score_right)
    my_pongtablev2.draw_score2(score_top)
    my_pongtablev2.draw_score3(score_bottom)

if(my_redball.x_posn + my_redball.width >= my_pongtablev2.width - 3):
    my_redball.stop_ball()
    my_redball.start_position()
    bat_L.start_position()
    bat_R.start_position()
    bat_T.start_position()
    bat_B.start_position()
    my_pongtablev2.move_item(bat_L.rectangle, 20, 250, 35, 350)
    my_pongtablev2.move_item(bat_R.rectangle, 575, 250, 590, 350)
    my_pongtablev2.move_item(bat_T.rectangle, 250, 15, 350, 30)
    my_pongtablev2.move_item(bat_B.rectangle, 250, 570, 350, 585) 
    score_right = score_right + 1
    if(score_right >= 10):
        score_right = "WINNER!!!"
        score_left = "Looser"
        score_top = "Looser"
        score_bottom = "Looser"
    first_serve = True
    my_pongtablev2.draw_score(score_left, score_right)
    my_pongtablev2.draw_score2(score_top)
    my_pongtablev2.draw_score3(score_bottom)


if(my_redball.y_posn <= 3):
    my_redball.stop_ball()
    my_redball.start_position()
    bat_L.start_position()
    bat_R.start_position()
    bat_T.start_position()
    bat_B.start_position()
    my_pongtablev2.move_item(bat_L.rectangle, 20, 250, 35, 350)
    my_pongtablev2.move_item(bat_R.rectangle, 575, 250, 590, 350)
    my_pongtablev2.move_item(bat_T.rectangle, 250, 15, 350, 30)
    my_pongtablev2.move_item(bat_B.rectangle, 250, 570, 350, 585)
    score_top = score_top + 1
    if(score_top >= 10):
        score_top = "WINNER!!!"
        score_left = "Looser"
        score_right = "Looser"
        score_bottom = "Looser"
    first_serve = True
    my_pongtablev2.draw_score3(score_bottom)
    my_pongtablev2.draw_score(score_left, score_right)
    my_pongtablev2.draw_score2(score_top)


if(my_redball.y_posn + my_redball.height >= my_pongtablev2.height - 3):
    my_redball.stop_ball()
    my_redball.start_position()
    bat_L.start_position()
    bat_R.start_position()
    bat_T.start_position()
    bat_B.start_position()
    my_pongtablev2.move_item(bat_L.rectangle, 20, 250, 35, 350)
    my_pongtablev2.move_item(bat_R.rectangle, 575, 250, 590, 350)
    my_pongtablev2.move_item(bat_T.rectangle, 250, 15, 350, 30)
    my_pongtablev2.move_item(bat_B.rectangle, 250, 570, 350, 585) 
    score_bottom = score_bottom + 1
    if(score_bottom >= 10):
        score_bottom = "WINNER!!!"
        score_right = "Looser"
        score_left = "Looser"
        score_top = "Looser"
    first_serve = True
    my_pongtablev2.draw_score2(score_top)
    my_pongtablev2.draw_score3(score_bottom)
    my_pongtablev2.draw_score(score_left, score_left)

my_redball.move_next()
window.after(17, game_flow)


def restart_game(master):
global score_left
global score_right
global score_top
global score_bottom
my_redball.start_ball(x_speed=x_velocity, y_speed=11)
if(score_left == "WINNER!!!" or score_right == "Looser" or  score_top == "Looser" or score_bottom == "Looser"):
    score_left = 0
    score_right = 0
    score_top = 0
    score_bottom = 0
my_pongtablev2.draw_score(score_left, score_right,)
my_pongtablev2.draw_score2(score_top)
my_pongtablev2.draw_score3(score_bottom)


 window.bind("q", bat_L.move_up)
 window.bind("a", bat_L.move_down)
 window.bind("<Up>", bat_R.move_up)
 window.bind("<Down>", bat_R.move_down)
 window.bind("u", bat_T.move_left)
 window.bind("i", bat_T.move_right)
 window.bind("n", bat_B.move_left)
 window.bind("m", bat_B.move_right)


window.bind("<space>", restart_game)


game_flow()


window.mainloop()

我假设 "start menu" 你指的是通常所说的 "toplevel menu"、"application menu" 或 "application menu-bar"。例如,在 MS-Windows 上,这将是一个出现在应用程序 window.

顶部的菜单

这里是 link 关于菜单的一些很好的帮助:The Tkinter Menu Widget. That site has plenty of other good info about Tkinter accessible from links on that page. Also, Tkinter 8.5 reference: a GUI for Python 是一个很好的资源。

但是为了解决您的具体问题,并使用您的代码,就在这两行之后:

window = Tk()
window.title("MyPong")

首先,添加这两行:

menu_bar = Menu(window)
window.config(menu=menu_bar)

这将创建应用程序菜单(又名菜单栏)。请注意 config() 函数(带有 menu=parameter)是在 Tk 对象 上调用的。这就是使它成为一个应用程序 window 的原因。 (您也可以将菜单与其他 windows 和小部件相关联,并使用相同的 Tk.Menu class 创建上下文菜单等。)

然后使用 Menu.add_cascade() 在此菜单栏上添加您想要的任何菜单。例如,"File" 菜单很常见:

file_menu = Menu(menu_bar)
menu_bar.add_cascade(label="File", menu=file_menu)

最后,您使用 Menu.add_command() 添加菜单命令并通过 command= 参数传递一个可调用对象:

file_menu.add_command(label="New", command=create_a_new_game)
file_menu.add_command(label="Open", command=open_a_saved_game)
file_menu.add_command(label="Save", command=save_the_game)
file_menu.add_separator()
file_menu.add_command(label="Exit", command=callback)

只需使用Menu.add_cascade()添加其他菜单。例如,另一个流行的菜单是 "Help":

help_menu = Menu(menu_bar)
help_menu.add_cascade(label="Help", menu=help_menu)
help_menu.add_command(label="About", command=show_help)

如果你想要子菜单,你可以在你添加到菜单栏的菜单上使用Menu.add_cascade()

file_submenu = Menu(file_menu)
file_menu.add_cascade(label="Submenu", menu=file_submenu)
file_submenu.add_command(label="Something", command=something)
file_submenu.add_command(label="Something Else", command=another)