简体   繁体   中英

Trying to create and configure multiple buttons from the same function in tkinter

so i'm trying to create a code which when each button is clicked it will make that button fit a certain style but with changes to the name, however my current code will replace the button you click and them when you click on another it disables the last one you clicked permanently is there anyway to make it so that the buttons all use the same function but dont get disabled when you activate another button?

from tkinter import *
MainWindow = Tk()
def SquadInfoBttnFull():
    global SquadFullBttn
    SquadFullBttn = Toplevel(MainWindow)
    SquadFullBttn.geometry("658x600")
# -- unit buttons -- #
    for i in range(10):
        Unit_Button = Button(SquadFullBttn, text="EMPTY", width=8, height=6)
        Unit_Button.grid(row = 0, column = i)
        Unit_Button.bind("<Button-1>", UnitFill)

def SquadInfoBttn(): 
    InfoBttn = Button(MainWindow, wraplength=50, justify=LEFT, text="SquadInfo Here", width=100, command=SquadInfoBttnFull) 
    InfoBttn.grid(row=0, column=0, sticky=W)

def UnitInfoBttn():
    UnitInfo = Toplevel(SquadFullBttn)
    UnitInfo.geometry("300x200")

def UnitFill(event):
    global photo
    photo = PhotoImage(file="csmSingle.png")
    btn = event.widget
    grid_info = event.widget.grid_info()
    btn = Button(SquadFullBttn, text="UNIT", image=photo, width=58, height=93, command=UnitInfoBttn, compound = TOP)
    btn.grid(row=grid_info["row"], column=grid_info["column"], sticky=E)

SquadInfoBttn()
MainWindow.mainloop()

You are trying to make changes to the existing button and not create a new one. Also, you don't need to create a new instance of PhotoImage every time. The following code works for me:

from tkinter import *

MainWindow = Tk()

photo = PhotoImage(file="csmSingle.png")

def SquadInfoBttnFull():
    global SquadFullBttn
    SquadFullBttn = Toplevel(MainWindow)
    SquadFullBttn.geometry("658x600")
# -- unit buttons -- #
    for i in range(10):
        Unit_Button = Button(SquadFullBttn, text="EMPTY", width=8, height=6)
        Unit_Button.grid(row = 0, column = i)
        Unit_Button.bind("<Button-1>", UnitFill)

def SquadInfoBttn(): 
    InfoBttn = Button(MainWindow, wraplength=50, justify=LEFT, text="SquadInfo Here", width=100, command=SquadInfoBttnFull) 
    InfoBttn.grid(row=0, column=0, sticky=W)

def UnitInfoBttn():
    UnitInfo = Toplevel(SquadFullBttn)
    UnitInfo.geometry("300x200")

def UnitFill(event):
    btn = event.widget
    grid_info = event.widget.grid_info()
    btn.config(text="UNIT", image=photo, width=58, height=93, command=UnitInfoBttn, compound = TOP)
    btn.grid(row=grid_info["row"], column=grid_info["column"], sticky=E)

SquadInfoBttn()
MainWindow.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM