Python Forum
[Tkinter] Help with tkinter; images and button commands
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Help with tkinter; images and button commands
#1
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
Reply
#2
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.
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] TKinter Remove Button Frame Nu2Python 8 803 Jan-16-2024, 06:44 PM
Last Post: rob101
  tkinter - touchscreen, push the button like click the mouse John64 5 739 Jan-06-2024, 03:45 PM
Last Post: deanhystad
  Problems trying to position images with Tkinter emont 3 668 Dec-12-2023, 07:20 AM
Last Post: menator01
  Centering and adding a push button to a grid window, TKinter Edward_ 15 4,371 May-25-2023, 07:37 PM
Last Post: deanhystad
  Can't get tkinter button to change color based on changes in data dford 4 3,361 Feb-13-2022, 01:57 PM
Last Post: dford
  Creating a function interrupt button tkinter AnotherSam 2 5,413 Oct-07-2021, 02:56 PM
Last Post: AnotherSam
  [Tkinter] Have tkinter button toggle on and off a continuously running function AnotherSam 5 4,918 Oct-01-2021, 05:00 PM
Last Post: Yoriz
  tkinter showing image in button rwahdan 3 5,517 Jun-16-2021, 06:08 AM
Last Post: Yoriz
  tkinter button image Nick_tkinter 4 3,955 Mar-04-2021, 11:33 PM
Last Post: deanhystad
  tkinter python button position problem Nick_tkinter 3 3,482 Jan-31-2021, 05:15 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020