我正在尝试将图像按钮更改为图像标签。按下按钮应将按钮的图像更改为与其他图像一起标记。之后,它应该仍然可以按下其他图像按钮。在
我有我从这里得到的代码:Python tkinter: Replacing an image button with an image label
from tkinter import *
class Game:
def __init__(self):
self.__window = Tk()
self.gifdir = "./"
self.igm = PhotoImage(file=self.gifdir+"empty.gif")
self.btn = Button(self.__window, image=self.igm, command = self.change_picture)
self.btn.grid(row=1, column=2, sticky=E)
self.btn2 = Button(self.__window, image=self.igm, command = self.change_picture)
self.btn2.grid(row=1, column=1, sticky=E)
self.__window.mainloop()
def change_picture(self):
self.igm = PhotoImage(file=self.gifdir+"new.gif")
self.btn.configure(image = self.igm)
def main():
Game()
main()
如果我按另一个按钮,我就不能再按另一个按钮了,我想把按下的按钮改成标签。在
我修改了代码,为按钮和图像使用多个引用:
对按钮和图像的引用存储在列表中。更改图片被更改为以按钮编号为参数,以便您可以区分是哪个按钮被按下。在
有了这些变化,每个按钮都可以单独按下,这样当按下时图像就会发生变化。在
相关问题 更多 >
编程相关推荐