繁体   English   中英

将 Label 移入 Tkinter

[英]Moving A Label In Tkinter

将 Label 移入 Tkinter

所以我正在 Tkinter 中创建一个类似于 Geometry Dash 的游戏,我想移动一个 label 这是主要角色,但我不知道如何。

我尝试使用 class 和 self. 主角和类似的东西,但它不起作用(至少我尝试过的方式)。 到目前为止,这是我的代码:

import tkinter as tk
from tkinter import *
from tkinter.ttk import *
import sys
import os
import time

# The main tk screen(startScreen)

startScreen = tk.Tk()
startScreen.title("The Quest")
avatar = PhotoImage(file='OneStop_Coding_100.png')
startScreen.iconphoto(False, avatar)

canvas1 = tk.Canvas(startScreen, bg='white', width=5000, height=5000)
startScreen.state('zoomed')
canvas1.pack()
startScreen.configure(bg='white')




# The story line that has the story
def storyLine():
    canvas1.delete("all")
    rounded_button_third.pack_forget()
    rounded_button_second.pack_forget()
    rounded_button.pack_forget()
    tos_l = tk.Button(startScreen, text='Start The Quest', bg='#BF9000', fg='#000', height=1, justify=tk.RIGHT,
                      command=lambda: [tos_l.pack_forget(), start_level()])
    tos_l.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
    # add button to startScreen window
    tos_l.pack(pady=10)
    load_image_back_sl = tk.PhotoImage(file="back_white.png")
    rounded_back_sl = tk.Button(image=load_image_back_sl)
    rounded_back_sl["bg"] = "white"
    rounded_back_sl["border"] = "0"
    rounded_back_sl.pack(side='top')
    rounded_back_sl['command'] = lambda: [rounded_back_sl.pack_forget(), restart_program()]
    S = tk.Scrollbar(startScreen)
    T = tk.Text(startScreen, height=500, width=5000, bg='white')
    canvas1.configure(bg='white', width=0, height=0)
    S.pack(side=tk.RIGHT, fill=tk.Y)
    T.pack(side=tk.TOP, fill=tk.Y)
    S.config(command=T.yview)
    T.config(yscrollcommand=S.set, state='disabled')
    # The story
    quote = """It was time...Recently you had found a note from your brother telling you what he had found. It read: I have found the key to defeating Teo. It might be surprising, but it is you. That was all that you had left of your brother and you desperately wanted to avenge him. So you start walking towards Teo's castle..."""
    T.config(state='normal')
    T.insert(tk.END, quote)
    T.config(state='disabled')
    # This button is not showing up...

    startScreen.update()
    startScreen.mainloop()

def howto():
    canvas1.delete("all")

    rounded_button_third.pack_forget()
    load_image_back_howto = tk.PhotoImage(file="back_white.png")
    rounded_back_howto = tk.Button(image=load_image_back_howto)
    rounded_back_howto["bg"] = "white"
    rounded_back_howto["border"] = "0"
    rounded_back_howto.pack(side='top')
    rounded_back_howto['command'] = lambda: [rounded_back_howto.pack_forget(), restart_program()]
    scroll_bar = tk.Scrollbar(startScreen)
    text_box = tk.Text(startScreen, height=500, width=5000, bg='white')
    canvas1.configure(bg='white', width=0, height=0)
    scroll_bar.pack(side=tk.RIGHT, fill=tk.Y)
    text_box.pack(side=tk.TOP, fill=tk.Y)
    scroll_bar.config(command=text_box.yview)
    text_box.config(yscrollcommand=scroll_bar.set, state='disabled')
    # The story
    howie = """To play this game just use the space bar to jump, or click the mouse, or press the up arrow or w. This is The Quest by the OneStop Coder(@ OneStop Coding). This game 
features tkinter python. Have any questions? Email the OneStop Coder @ hexafandra1405@gmail.com. Have fun!"""
    text_box.config(state='normal', font='myFont')
    text_box.insert(tk.END, howie)
    text_box.config(state='disabled')
    # This button is not showing up...

    startScreen.update()
    startScreen.mainloop()
def restart_program():
    """Restarts the current program.
    Note: this function does not return. Any cleanup action (like
    saving data) must be done before calling this function."""
    python = sys.executable
    os.execl(python, python, * sys.argv)

def about_def():
    canvas1.delete("all")
    load_image_back = tk.PhotoImage(file="back_white.png")
    rounded_back = tk.Button(image=load_image_back)
    rounded_back["bg"] = "white"
    rounded_back["border"] = "0"
    rounded_back.pack(side='top')
    rounded_back['command'] = lambda: [rounded_back.pack_forget(), restart_program()]
    scroll_bar = tk.Scrollbar(startScreen)
    text_box = tk.Text(startScreen, height=500, width=5000, bg='white')
    canvas1.configure(bg='white', width=0, height=0)
    scroll_bar.pack(side=tk.RIGHT, fill=tk.Y)
    text_box.pack(side=tk.TOP, fill=tk.Y)
    scroll_bar.config(command=text_box.yview)
    text_box.config(yscrollcommand=scroll_bar.set, state='disabled')
    # The story
    about = """This is The Quest A OneStop Coding Productions. OneStop Coding thrives to create games that YOU enjoy. """
    text_box.config(state='normal', font='myFont')
    text_box.insert(tk.END, about)
    text_box.config(state='disabled')
    startScreen.update()
    startScreen.mainloop()
# The start Level that I need help on
def start_level():

    def intro():
        size = 40
        canvas1.create_text(x + 500, y, text='  The Quest', font=("Century", size), fill='black')

    avatar_logo.destroy()
    intro()
    rounded_button.pack(side='top')
    rounded_button_second.pack(side='top')
    rounded_button_third.pack(side='top')
    rounded_button.pack_forget()
    rounded_button_second.pack_forget()
    rounded_button_third.pack_forget()
    mainCharX = 680
    mainCharY = 700
    z = mainCharX + 70
    a = z
    canvas1.configure(bg='blue', width=5000, height=1000)
    mainChar_load = tk.PhotoImage(file="main_Char.png")
    bottom_ground = canvas1.create_rectangle(0, 755, 2000, 758, fill='black')

    def obstacle_triangle():
        one = 1300
        two = 755
        three = 1310
        four = 735
        five = 1320
        points = [1000, 755,  1010, 735, 1020, 755]
        canvas1.create_polygon(one, two, three, four, five, two, fill='black')
        one += 10
        two += 10
        three += 10
        four += 10
        five += 10

    obstacle_triangle()


    main_char =  Label(startScreen, image=mainChar_load)
    main_char.place(x = 750, y = 700) # sample main character


    startScreen.update()
    startScreen.mainloop()


x = 230
y = 100
z = x + 50
a = x + 50

avatar_logo = Label(image = avatar)
avatar_logo.place(x = 710, y = 170)
startText = canvas1.create_text(x + 500, y, text='  The Quest', font=("MS Gothic", 40), fill='black')

oneStopCoding = canvas1.create_text(x + 530, y + 40, text='A OneStop Coding Production', font=("MS Gothic", 10), fill='black')

line = canvas1.create_rectangle(0, 300, 5000, 500, fill='white')
load_image = tk.PhotoImage(file="start_white.png")
rounded_button = tk.Button(image=load_image)
rounded_button["bg"] = "white"
rounded_button["border"] = "0"
rounded_button.pack()
rounded_button.place(relx=0.5, rely=0.5, anchor=tk.CENTER)
rounded_button['command']=lambda:[rounded_button.pack_forget(), storyLine()]
load_image_second = tk.PhotoImage(file="how_white.png")
rounded_button_second = tk.Button(image=load_image_second, justify=tk.CENTER)
rounded_button_second["bg"] = "white"
rounded_button_second["border"] = "0"
rounded_button_second['command']=lambda: [rounded_button_second.pack_forget(), howto(), rounded_button.pack_forget()]
rounded_button_second.pack()
rounded_button_second.place(relx=0.6, rely=0.5, anchor=tk.CENTER)
load_image_third = tk.PhotoImage(file="about_white.png")
rounded_button_third = tk.Button(image=load_image_third)
rounded_button_third["bg"] = "white"

rounded_button_third["border"] = "0"
rounded_button_third.pack()
rounded_button_third.place(relx=0.4, rely=0.5, anchor=tk.CENTER)
rounded_button_third['command']=lambda:[rounded_button_third.pack_forget(), about_def()]
startScreen.update()
startScreen.mainloop()

main_char 或

main_char = Label(startScreen, image=mainChar_load)
main_char.place(x = 750, y = 700) # sample main character

是我要移动的那个。

如果您只想编辑一个坐标(如果您想同时编辑两个坐标也可以),您可以使用main_char.place_configure(x=... or y=...) ) 或使用main_char.place(x=..., y=...)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM