0

I'm trying to get the last few lines of the following code to have when one of the original 6 buttons is pressed to call the appropriate function to rename the buttons. I've tried changing the command line to buttons[0].command = Pistols(). I've also tried using a if loop with a variable such as x == 1 to determine that if the button is pressed x with then be 1 and the for loop will call the function Pistols, but with no success. However the button automatically calls the function and renames the first button to ".44 Pistol" rather than what it should be "Pistols". I wan't the command to only be executed and call the function when pressed. I know that tkinter will automatically look to the function being called and run it's code. How can I either delay this or go about this in another way to have the functions code only execute when pressed. Thanks in advance!

   from tkinter import *
buttons = []
clm = [1,2,1,2,1,2]
rw = [1,1,2,2,3,3]
btnmain_list = ['Pistol','Rifle','Assult Rifle','Submachine Gun','Heavy Weapon','Plasma Weapons']
btnpistol_list = ['.44 Pistol', '10mm Pistol', 'Pipe Bolt-Action Pistol','Flare Gun', 'Pipe Pistol', 'Pipe Revolver']
btnrifle_list = []
btnasrifle_list = []
btnsubgun_list = []
btnheavy_list = []
btnplasma_list = []
ms = Tk()
ms.title('Fallout 4 weapon mods and needed materials')
ms.geometry('450x400')
placement = Frame(ms)
placement.grid()

class Guns:

    def Pistols ():
        buttons[0] = Button(placement,height = '5',width = '20', text = btnpistol_list[0])
        buttons[0].grid(column = clm[0], row = rw[0])

    def Rifles ():
        x = 0

    def AssultRifles ():
        x = 0
    def SubmachineGuns ():
        x = 0

    def HeavyWeapons ():
        x = 0

    def PlasmaWeapons ():
        x = 0

    for i in range (6):
        b = Button(placement,height = '5',width = '20', text = btnmain_list[i])
        b.grid(column = clm[i], row = rw[i])
        buttons.append(b)
    buttons[0].command = Pistols()
2
  • 1
    I ran your code and it does not automatically call "Pistols" as you have described. Please provide a minimal reproducible example that actually demonstrates your problem. Commented Dec 8, 2015 at 15:00
  • It does, it changes the name of the first button to ".44 Pistol" on start up. There is nothing wrong with the description of their problem. I have described the solution below. Commented Dec 8, 2015 at 15:24

1 Answer 1

1

I've found a solution by changing the class to this:

class Guns:
    global counter
    counter = 0

    def pistolCycle():
        global counter


        buttons[0].config(text=btnpistol_list[counter])

        if counter == len(btnpistol_list)-1:
            counter=0

        counter = counter+1

    def Pistols ():

        buttons[0] = Button(placement, height = '5',width = '20', text="Pistols", command = lambda: Guns.pistolCycle() )
        buttons[0].grid(column = clm[0], row = rw[0])


    def Rifles ():
        x = 0

    def AssultRifles ():
        x = 0
    def SubmachineGuns ():
        x = 0

    def HeavyWeapons ():
        x = 0

    def PlasmaWeapons ():
        x = 0

    for i in range (6):
        b = Button(placement,height = '5',width = '20', text = btnmain_list[i])
        b.grid(column = clm[i], row = rw[i])
        buttons.append(b)

    Pistols()

So, here's a breakdown of what happens:

  1. Once your buttons are defined, the Pistol function is called, which adds all the features to your Pistol button, including changing the text, and adding the function it will call when pressed.
  2. When the button is pressed, it calls to pistolCycle. What pistol cycle does, is takes the "counter" value, and changes the text of the button to the item in the list which is associated to it. EG, when the counter is 0, .44 Pistol is displayed.
  3. The counter increases by one, each time pistolCycle is called, meaning the next time it's called, it will display the next item in the list.

Now, using global variables can get messy. I've given you the basic framework, so you may be able to use your own logic to get the variable "counter" to pass into pistolCycle each time (EG, pistolCycle(counter))

You will need to make a separate counter and cycle function in order for all the buttons to work.

I hope this helped!!

PS: The if statement in the pistolCycle function means that it wont try and get an item when it doesn't exist in the list.

Sign up to request clarification or add additional context in comments.

5 Comments

I've changed the line of code as you have suggested. But when the button is pressed nothing happens now.
I see, are you trying to cycle the pistol names when the button is clicked? I will try when I get home.
Yes, the original screen you see will eventually function as the "Main" or "Home" forum. The functions i'm trying to call will also contain a button to return to the main forum. If it would help, I could email or message you more details about exactly what I am trying to have the program do or what the programs functionality is.
Edited, check it out.
Thank you! This has actually sparked an innovation to my entire way of going about this program. I'm making some big changes in my format and am going to use this to assist me. I love the explanation of everything to, helped clear some things up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.