Python Forum
Help w/ variables and destroying buttons
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help w/ variables and destroying buttons
#1
Good Day, i'm making a game for python. I have a loading screen and then I have a lobby where you choose the character. When a character is picked it's supposed to set the character value to the character number and erase all the button, currently only one. The problem is the button is not erased when pressed on. It's stumped me for awhile so I came here. I hope it's no just some simple spelling error. Thanks in Advance!
from tkinter import *


#Variables
tk = Tk()
money = 0
score = 0
startvariable = 0
CharacterVariable = 0
ShotonButton = Button()

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

#Classes
def StartUpSprites():
    class StartingSprites():
        class Shoton():
            ShotonButton = Button(tk, text="", command=startgame(1), font=('Times', 50), bg='red', foreground='blue')
            Shoton = ShotonButton
            Shoton.config(image=ShotonImage, width=130, height=200 )
            Shoton.pack()
            Shoton.place(relx=.1, rely=.1, anchor='nw')

def StartUpItems():
    class StartingItems():
        pass

def StartUpBG():
    class FightingBG():
        canvas.create_image(0, 0, anchor=NW, image=BGImage)

#Canvas Creator
canvas = Canvas(tk, width=800, height=800, bg='white')
canvas.pack()
StartUpBG()

#Functions
def startscreen():
    canvas.delete('all')
    startbtn.destroy()
    StartUpBG()
    StartUpSprites()
    StartUpItems()
    startvariable = 1

def findCharacter():
    if CharacterVariable == 1:
        print("Chungus")

def startgame(character):
    ShotonButton.destroy()
    CharacterVariable = character
    findCharacter()

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

#Canvas Text/Images
GunnerText = canvas.create_text(400, 300, text='The Gunner', fill='red', font=('Times', 100))
Reply
#2
Shoton.pack()
Shoton.place(relx=.1, rely=.1, anchor='nw')
which is it, pack or place you can't mix geometry types on same container
and you cannot have more than one per widget
Reply
#3
Ok, thx for telling me that, but it still won't destroy
Reply
#4
what's with embedded classes?
This is very odd, and I expect incorrect use of classes
Reply
#5
So I'm making a fighting game and I have a couple problems. First off when the button is pressed it is supposed to trigger a chain of functions which does happen, but it doesn't destroy the buttons as shown on lines 59 and 60. Another problem I found is, as shown in line 61 on the CharacterSelection function, it tells the game what character to pick based on that variable. It is supposed to change the variable inside the function before sending it off to another function. But instead, the variable takes its value from the top. On line 65, the character is printed because I changed it to if the variable is 0. This show it is taking its value given to it at the top. Can someone please tell me how to fix this. Thanks in Advance!

from tkinter import *


#Variables
tk = Tk()
money = 0
score = 0
startvariable = 0
ShotonButton = Button()
Shoton = Button()
Something = Button()
findAssist = 0
CharacterVariable = 0

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

#Classes
def StartUpSprites():
    class StartingSprites():
        class 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))
        class Something():
            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)
            ShotonLabel = canvas.create_text(310, 310, text='PoopHead', fill='Brown', font=('Times', 25))
            

def StartUpItems():
    class StartingItems():
        pass

def StartUpBG():
    class FightingBG():
        canvas.create_image(0, 0, anchor=NW, image=BGImage)

#Canvas Creator
canvas = Canvas(tk, width=800, height=800, bg='white')
canvas.pack()
StartUpBG()

#Functions
def startscreen():
    canvas.delete('all')
    startbtn.destroy()
    StartUpBG()
    StartUpSprites()
    startvariable = 1

def CharacterSelection(character):
    Shoton.destroy()
    Something.destroy()
    CharacterVariable = character
    findCharacter(findAssist)

def findCharacter(variable2):
    if CharacterVariable == 0:
        print("Shoton")
        if variable2 == 1:
            StartingSprites()

def CharacterSelection2(character):
    Shoton.destroy()
    Something.destroy()
    

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

#Canvas Text
GunnerText = canvas.create_text(400, 300, text='The Gunner', fill='red', font=('Times', 100))
Reply
#6
Why are you re-posting this, merge with thread: https://python-forum.io/Thread-My-button-won-t-destroy
Double posting is against forum rules.
Reply


Forum Jump:

User Panel Messages

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