Bottom Page

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
Age Doesn't Matter, Your Passion Does
- Shreya Jain
Quote
#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.
Quote
#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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  tkinter button not accessing the command when clicked jhf2 1 109 Nov-23-2019, 10:17 PM
Last Post: DT2000
  Tkinter button in for Simpsunt 3 98 Nov-23-2019, 01:41 AM
Last Post: Larz60+
  tkInter and Pillow don't display any images in GUIs - program gives errors instead SomeRandomGuy 9 343 Oct-29-2019, 02:57 PM
Last Post: SomeRandomGuy
  [Tkinter] Checking button click in Tkinter GalaxyCoyote 3 233 Oct-20-2019, 03:28 AM
Last Post: GalaxyCoyote
  [Tkinter] Tkinter button help and general commands Iceman 1 236 Sep-01-2019, 05:09 PM
Last Post: Yoriz
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 405 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] Tkinter window pop up again when i click button Orimura_Sandy 1 614 May-12-2019, 08:17 PM
Last Post: joe_momma
  tkinter- adding a new window after clicking a button built on the gui ShashankDS 2 977 Apr-18-2019, 12:48 PM
Last Post: ShashankDS
  Tkinter Button Settings kaanertugrul 3 1,134 Jan-25-2019, 03:41 PM
Last Post: Larz60+
  Remove duplicate images - tkinter? darter 5 1,000 Nov-10-2018, 10:54 PM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 2 Guest(s)