pythontkinter

Moving A Label In Tkinter


Moving A Label In Tkinter

So I am creating a game in Tkinter similar to Geometry Dash and I want to move a label which is the main Character but I don't know how.

I tried using a class and self. main character and stuff like that but it does not work(at least the way that I have tried it). Here it my code so far:

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

The main_char or

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

is the one that I want to move.


Solution

  • You can do it with main_char.place_configure(x=... or y=...) if you only want to edit one coordinate (also works if you want to edit both) or with main_char.place(x=..., y=...)