简体   繁体   English

尝试从Tkinter中的同一功能创建和配置多个按钮

[英]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. 另外,您不需要每次都创建一个新的PhotoImage实例。 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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