如何在顶部添加tkinter下拉菜单,并具有更改(乌龟)屏幕背景颜色的选项?

时间:2019-05-11 21:40:34

标签: python tkinter turtle-graphics maze

因此,我有一个使用tkinter运行乌龟图形的程序。我想要一个下拉菜单,例如具有“文件”的菜单,并且下拉菜单具有“另存为...”之类的内容。除了我想说的是“编辑”,下拉菜单中有“颜色”的选项,如果他们单击它,则会弹出带有不同颜色的按钮选项的窗口,然后单击按钮会将屏幕的背景色更改为任何颜色设置按钮的颜色。

我无法弄清楚。我尝试查找这些下拉菜单的示例,但似乎没有任何尝试过。我会详细描述我尝试过的内容,但老实说,这不仅耗时太长,而且我什至不了解自己尝试过的内容。请帮忙!我在下面附加了我的整个游戏代码,这样每个人都可以看到我到目前为止的情况。

from turtle import RawTurtle, TurtleScreen
import tkinter

largefont = ("Verdana", 12)

# Define function to create Spanish level.
def span():
    pen.setup(level_1)
    player.setup(level_1)
    setup(level_1)

# Define function to create French level.
def fren():
    pen.setup(level_2)
    player.setup(level_2)
    setup(level_2)

# Define function to create Japanese level.
def jpn():
    pen.setup(level_3)
    player.setup(level_3)
    setup(level_3)

# Define function to create Turkish level.
def turk():
    pen.setup(level_4)
    player.setup(level_4)
    setup(level_4)

# Create class with separate window to choose level.
class StartPage():
    def __init__(self):
        # Creation of second window.
        wn = tkinter.Tk()
        wn.title("Welcome!")
        # Creation of game title on start page.
        label = tkinter.Label(wn, text="Language Labyrinth", font=largefont)
        label.pack()

        # Create Spanish level button.
        button = tkinter.Button(wn, text="Spanish", command=span)
        button.pack()

        # Create French level button.
        button2 = tkinter.Button(wn, text="French", command=fren)
        button2.pack()

        # Create Japanese level button.
        button3 = tkinter.Button(wn, text="Japanese", command=jpn)
        button3.pack()

        # Create Turkish level button.
        button4 = tkinter.Button(wn, text="Turkish", command=turk)
        button4.pack()

        # Create quit button for start page.
        qbutton = tkinter.Button(wn, text="Quit", command=wn.destroy)
        qbutton.pack()

start = StartPage()

# Create Pen class to draw the maze.
class Pen(RawTurtle):
    def __init__(self):
        super().__init__(screen, shape='square')
        self.speed('fastest')
        self.color('white')
        self.penup()

    # Create setup so the maze will be drawn.
    def setup(self, level):
        for y in range(len(level)):
            screen_y = 288 - (y * 24)

            for x in range(len(level[y])):
                if level[y][x] == 'X':
                    screen_x = (x * 24) - 288

                    self.goto(screen_x, screen_y)
                    self.stamp()

                    walls.append((screen_x, screen_y))

# Create player class to have a player.
class Player(RawTurtle):
    def __init__(self):
        super().__init__(screen, shape='square')
        self.speed('fastest')
        self.color('blue')
        self.penup()

    # Create setup to create the player on the screen.
    def setup(self, level):
        for y in range(len(level)):
            for x in range(len(level[y])):
                if level[y][x] == 'P':
                    screen_x = (x * 24) - 288
                    screen_y = 288 - (y * 24)

                    self.goto(screen_x, screen_y)

                    return

    # Define a function that will allow player to move up.
    def move_up(self):
        # Calculate the spot to move to.
        movetoX = self.xcor()
        movetoY = self.ycor() + 24

        # Check if the space has a wall.
        if (movetoX, movetoY) not in walls:
            self.goto(movetoX, movetoY)

            gold_encounter()

    # Define a function that will allow player to move down.
    def move_down(self):
        # Calculate the spot to move to.
        movetoX = self.xcor()
        movetoY = self.ycor() - 24

        # Check if the space has a wall.
        if (movetoX, movetoY) not in walls:
            self.goto(movetoX, movetoY)

            gold_encounter()

    # Define a function that will allow player to move left.
    def move_left(self):
        # Calculate the spot to move to.
        movetoX = self.xcor() - 24
        movetoY = self.ycor()

        # Check if the space has a wall.
        if (movetoX, movetoY) not in walls:
            self.goto(movetoX, movetoY)

            gold_encounter()

    # Define a function that will allow player to move right.
    def move_right(self):
        # Calculate the spot to move to.
        movetoX = self.xcor() + 24
        movetoY = self.ycor()

        # Check if the space has a wall.
        if (movetoX, movetoY) not in walls:
            self.goto(movetoX, movetoY)

            gold_encounter()

    # Check if player touches the question.
    def collision(self, other):
        return self.distance(other) < 5

# Create Question class to create the "gold" in the game.
class Question(RawTurtle):
    def __init__(self, x, y):
        super().__init__(screen, shape='circle', visible=False)

        self.speed('fastest')
        self.color('gold')
        self.penup()
        self.goto(x, y)
        self.showturtle()

    # Define function that will remove gold when collided with.
    def destroy(self):
        self.hideturtle()

# Define function to setup the "gold" in the game.
def setup(level):
    for y in range(len(level)):
        for x in range(len(level[y])):
            char = level[y][x]

            screen_x = -288 + (x * 24)
            screen_y = 288 - (y * 24)

            if char == 'Q':
                questions.append(Question(screen_x, screen_y))

# Define a function for the quit button.
def quitPlaying():
    root.destroy()
    root.quit()

# Game loop in regards to the gold.
def gold_encounter():
    # Check for player collision with a question.
    # Iterate through the questions list.
    for question in questions:
        if player.collision(question):
            # Destroy the question.
            question.destroy()
            # Remove question from questions list.
            questions.remove(question)

# Create window and canvas using tkinter.
root = tkinter.Tk()
root.title("Language Labyrinth")

canvas = tkinter.Canvas(root, width=600, height=600)
canvas.pack()

screen = TurtleScreen(canvas)
screen.bgcolor('black')

# Create frame where button(s) will be.
frame = tkinter.Frame(root)
frame.pack()

# Add questions list.
questions = []

# Wall coordinate list.
walls = []

# Create a levels list.
levels = []

# Define first level.
level_1 = [
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"XP XXXXXXX          XXXXX",
"X  XXXXXXX  XXXXXX  XXXXX",
"X       XX  XXXXXX  XXXXX",
"X       XX  XXX        XX",
"XXXXXX  XX  XXX   Q    XX",
"XXXXXX  XX  XXXXXX  XXXXX",
"XXXXXX  XX    XXXX  XXXXX",
"X  XXX Q      XXXX  XXXXX",
"X  XXX  XXXXXXXXXXXXXXXXX",
"X         XXXXXXXXXXXXXXX",
"X     Q          XXXXXXXX",
"XXXXXXXXXXXX     XXXXX  X",
"XXXXXXXXXXXXXXX  XXXXX  X",
"XXX  XXXXXXXXXX         X",
"XXX               Q     X",
"XXX         XXXXXXXXXXXXX",
"XXXXXXXXXX  XXXXXXXXXXXXX",
"XXXXXXXXXX              X",
"XX   XXXXX        Q     X",
"XX   XXXXXXXXXXXXX  XXXXX",
"XX    XXXXXXXXXXXX  XXXXX",
"XX    Q     XXXX        X",
"XXXX                    X",
"XXXXXXXXXXXXXXXXXXXXXXXXX"
]

# Define second level.
level_2 = [
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"XP  XX       XX      XXXX",
"X   XX       XX      XXXX",
"X   XXXXXXX  XX  XX     X",
"X   XXXXXXX  XX  XX Q   X",
"X   XX   XX  XX  XXXXX  X",
"X   XX   XX  XX  XXXXX  X",
"XQ         Q     XX  X  X",
"X                XX  X  X",
"X   XXXXXXX   XXXXX  XXXX",
"X   XX   XX             X",
"XXXXXX   XX        Q    X",
"XXXXXX   XXXXXXXXXXXX   X",
"X      Q      XX   XX   X",
"X             XX   XX   X",
"XXXXXXXXXX    XX        X",
"XXXXXX   X    XX     Q  X",
"XXXXXX   X    XX   XX   X",
"X                 X  X  X",
"X       Q         X  X  X",
"XXXXXX   XXXXXXXXXX  X  X",
"XXXXXX   XXXXXXXXXX     X",
"X         X             X",
"X         XQ     XXXXXXXX",
"XXXXXXXXXXXXXXXXXXXXXXXXX"
]

# Define third level.
level_3 = [
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"X      X   XP           X",
"X    Q X   X            X",
"X   XXXX   XXXXXXXXXX   X",
"X   XXXX   XXXXXXXXXX   X",
"X         Q    XX       X",
"X              XX       X",
"XXXXXXXXXXXX   XXXX   XXX",
"X              XXXX   XXX",
"X              XXXX   XXX",
"XXXXX Q XXXXXXXXXXX   XXX",
"X                   Q   X",
"X                       X",
"XXXXXXXXXX Q  XXXXXXX   X",
"XXXXXXXXXX    XXXXXXX   X",
"XXXX         XXXXXXXXX  X",
"XXX      XXXXXXXXX      X",
"XXXXXX   XXXXXXXXXXXX   X",
"X            X      Q   X",
"X  Q         X       XXXX",
"XXXXXXX            XXXXXX",
"XXXXXXXXXXXX Q XXXXXXXXXX",
"X                       X",
"X Q         XXX       Q X",
"XXXXXXXXXXXXXXXXXXXXXXXXX"
]

# Define fourth level.
level_4 = [
"XXXXXXXXXXXXXXXXXXXXXXXXX",
"XXXXXXXXXX  P  XXXXXXXXXX",
"XXXXXXXXXX     XXXXXXXXXX",
"XXXXXXXXXXX Q XXXXXXXXXXX",
"X                       X",
"X          XXX          X",
"XXXXX  Q  XXXXX  Q  XXXXX",
"X          XXX          X",
"XXXXXXXX    X    XXXXXXXX",
"X Q         X         Q X",
"X           X           X",
"XXXXXXXXXX QXQ XXXXXXXXXX",
"X                       X",
"X                       X",
"XXXXXXXXXXXXXXXXXXXXX   X",
"XXXXXXXXXXXXXXXXXXXXX   X",
"XXXXXX   XXXXXXXXX      X",
"XXXXXX   XXXXXXX Q  XXXXX",
"XQ                      X",
"X                       X",
"XXXXXXXXXXX   XXXXXXX   X",
"XXXXXXXX      XXXXXXX   X",
"XXXXXX     XXXXXXXXXX   X",
"XXXXX  Q XXXXXXXXXXX Q  X",
"XXXXXXXXXXXXXXXXXXXXXXXXX"
]

# Add the level(s) to the levels list.
levels.append(level_1)
levels.append(level_2)
levels.append(level_3)
levels.append(level_4)

# Class instances.
pen = Pen()
player = Player()

# Creation of quit button.
quitButton = tkinter.Button(frame, text='Quit', command=quitPlaying)
quitButton.pack()

screen.onkeypress(player.move_up, 'Up')
screen.onkeypress(player.move_down, 'Down')
screen.onkeypress(player.move_left, 'Left')
screen.onkeypress(player.move_right, 'Right')

screen.listen()

# Call main game loop.
screen.mainloop()

因此,基本上,我希望在左上角有一个下拉菜单,其中显示“编辑”,然后下拉以看到“颜色”,如果单击它,将打开一个窗口并允许用户选择通过单击按钮选项之一来显示屏幕背景的颜色。但是,我不知道该怎么办。

0 个答案:

没有答案