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 command tkinter Heyjoe 6 481 Jul-30-2020, 07:06 PM
Last Post: deanhystad
  [tkinter] color change for hovering over button teacher 4 177 Jul-04-2020, 06:33 AM
Last Post: teacher
  [Tkinter] Loading Images to Tkinter with Pillow Tomli 2 238 Jun-18-2020, 03:26 AM
Last Post: Tomli
  tkinter button help Gina92 10 407 May-27-2020, 08:17 AM
Last Post: Gina92
  How to make button text bold in Tkinter? scratchmyhead 2 255 May-16-2020, 02:53 AM
Last Post: scratchmyhead
  Images in tkinter menator01 0 191 Apr-25-2020, 12:49 AM
Last Post: menator01
  [Tkinter] Is it possible to automate button generation in tkinter? FirePepi 3 353 Apr-10-2020, 12:14 PM
Last Post: Riddle
  Tkinter:Unable to bind and unbind function with a button shallanq 2 392 Mar-28-2020, 02:05 AM
Last Post: joe_momma
  Returning a value from a tkinter.button call markr0804 4 1,787 Feb-16-2020, 10:35 AM
Last Post: markr0804
  Problem with Submit button Tkinter Reldaing 2 411 Jan-05-2020, 01:58 AM
Last Post: balenaucigasa

Forum Jump:


Users browsing this thread: 1 Guest(s)