Python Forum

Full Version: Help with tkinter; images and button commands
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm making a game and I have a few problems. The first is that the BG image shows up when the game is opened. I really just want it to open when the button is clicked. The second problem is the button is not triggering the command when it is pressed. It is supposed to delete everything and pull up button of the characters.
Here's the code
from tkinter import *

#Variables
tk = Tk()
ShotonButton = Button()
Shoton = Button()
Something = Button()
startbtn = Button()
CharacterVariable = 0
CharacterVariable2 = 0

#Images
ShotonImage = PhotoImage(file='C:/Users/Chadd/Pictures/Converted/Converted/Official Shoton.gif')
SomethingImage = PhotoImage(file='C:/Users/Chadd/Pictures/Converted/Character 1.gif')
BGImage = PhotoImage(file='C:/Users/Chadd/Downloads/Background.gif')

#Sprites
class Sprites():
    def Shoton():
        ShotonButton = Button(tk, text="", command=CharacterSelection(1), bg='red')
        Shoton = ShotonButton
        Shoton.config(image=ShotonImage, width=130, height=200 )
        Shoton.place(relx=.1, rely=.1)
        ShotonLabel = canvas.create_text(145, 310, text='Shoton', fill='red', font=('Times', 30))
    def Poop():
        Something = Button(tk, text="", command=CharacterSelection(2), bg='Brown')
        sg = Something
        sg.config(image=SomethingImage, width=130, height=200)
        sg.place(relx=.3, rely=.1)
        sgLabel = canvas.create_text(310, 310, text='PoopHead', fill='Brown', font=('Times', 25))

class Fighter():
        print('Finished')


#Canvas
class GameCanvas():
    def BackGround():
        canvas.create_image(0, 0, anchor=NW, image=BGImage)
    def CharSelInter():
        canvas.delete('all')
        startbtn.destroy()
        GameCanvas.BackGround()
        Sprites()

#Character Selection
class Char():
    def CharSel(character):
        Shoton.destroy()
        Something.destroy()
        canvas.delete('all')
        GameCanvas.BackGround()
        CharacterVariable = character
        findCharacter(0, character)

    def CharSel2(character):
        Shoton.destroy()
        Something.destroy()
        canvas.delete('all')
        GameCanvas.BackGround()
        Char.findChar(1, character)
        CharacterVariable2 = character

    def findChar(value, char):
        print(char)
        if value == 1:
            Fighter()
        else:
            CharSel2(character)
            Fighter()

#Start Screen
canvas = Canvas(tk, width=800, height=800, bg='blue')
canvas.pack()

startbtn = Button(tk, text="Start Game", command=GameCanvas.CharSelInter(), font=('Times', 50), bg='red', foreground='blue')
startbtn.pack()
startbtn.place(relx=.5, rely=.8, anchor="c")

GunnerText = canvas.create_text(400, 300, text='The Gunner', fill='red', font=('Times', 100))
Thanks in Advance
Command bindings should not call the event handler function/method ie take the () off the end, otherwise they are called imediately and the result of the call is bound to the event.
In addition, use partial to send args to a function
from functools import partial
...
## there is no function CharacterSelection in the code you posted
ShotonButton = Button(tk, text="", command=partial(CharacterSelection, 1), bg='red')
when you have time, peruse the Python Style Guide, function names are all_lower_case_with_underscores.