使用函数创建 tkinter 按钮

huangapple go评论63阅读模式
英文:

Create tkinter buttons using a function

问题

当我尝试使用函数(new_btn(r, c, btn))在网格中创建按钮时,它们上面的图像被移除,我只能点击最后一个按钮。这是一个9x9的网格。

我尝试使用循环,但然后我无法设置按钮的行和列,这很重要,因为在这个项目的后续部分中只有一个按钮应该有图像。

def new_btn(r, c, btn):
    global photo
    # 创建按钮
    
    btn_1.update()
    print(btn)
    btn = Button(root, image=get_img(btn_1),)
    
    print(btn.winfo_height())
    btn.grid(row=r, column=c, sticky="NESW")
    return btn

btn_1 = Button(root, text="Button 1",)
btn_1.grid(row=0, column=0)
button_1 = new_btn(0, 0, "1")
button_2 = new_btn(0, 1, "2")
button_3 = new_btn(0, 2, "3")
button_4 = new_btn(1, 0, "4")
button_5 = new_btn(1, 1, "5")
button_6 = new_btn(1, 2, "6")
button_7 = new_btn(2, 0, "7")
button_8 = new_btn(2, 1, "8")
button_9 = new_btn(2, 2, "9")

def get_img(button):
    global resize_image
    global photo
    global abs_file_path
    img = Image.open(abs_file_path)

    resize_image = img.resize((button.winfo_height(), button.winfo_height()))
    photo = ImageTk.PhotoImage(resize_image)
    return photo

get_img函数只是创建图像的文件路径并将其大小设置为按钮的大小。请注意,在我的代码中,get_img位于其他函数之上,如果这是相关的话。abs_file_path变量在此之前已创建,它只是图像的路径。

英文:

When i try to create buttons in a grid using a function (new_btn(r,c,btn)), the image on them gets removed and i cant click them exept for the last one. Its a 9*9 grid.

I tried it using loops but then i was not able to set the row an colum of the button, this is important because later on in this project only one button should have the image on.

def new_btn(r,c,btn):
    global photo
    # Create Buttons
    
    btn_1.update()
    print(btn)
    btn = Button(root, image=get_img(btn_1),)
    
    print(btn.winfo_height())
    btn.grid(row=r,column=c,sticky="NESW")
    return btn

btn_1 = Button(root,text="Button 1",)
btn_1.grid(row=0,column=0)
button_1 =  new_btn(0,0,"1")
button_2 =  new_btn(0,1,"2")
button_3 =  new_btn(0,2,"3")
button_4 =  new_btn(1,0,"4")
button_5 =  new_btn(1,1,"5")
button_6 =  new_btn(1,2,"6")
button_7 =  new_btn(2,0,"7")
button_8 =  new_btn(2,1,"8")
button_9 =  new_btn(2,2,"9")

def get_img(button):
    global resize_image
    global photo
    global abs_file_path
    img = Image.open(abs_file_path)

    resize_image = img.resize((button.winfo_height(), button.winfo_height()))
    photo = ImageTk.PhotoImage(resize_image)
    return photo

the get_image function just creates the file path to the image and sets its size to the size of the button. Note that in my code the get_img is above the other function if that is even relevant. The variable abs_file_path is created before it is just the path to the image.

答案1

得分: 0

自从您在get_img()函数内使用了相同的全局变量photo来存储图像,所以只会显示最后一个按钮的图像,因为其他图像由于没有变量引用而被垃圾回收了。

您需要通过使用这些按钮的属性来保存所有按钮的图像引用:

def new_btn(r, c, btn):
    ...
    image = get_img(btn_1)
    btn = Button(root, image=image)
    btn.image = image # 使用按钮的属性保存图像引用
    ...
英文:

Since you have used same global variable photo to store the image inside get_img(), so only the image of the last button will be shown because the other images are garbage collected due to no variable referencing them.

You need to save the reference of the image for all the buttons by using an attribute of those buttons:

def new_btn(r, c, btn):
    ...
    image = get_img(btn_1)
    btn = Button(root, image=image)
    btn.image = image # save the reference of image using an attribute of the button
    ...

huangapple
  • 本文由 发表于 2023年3月31日 17:04:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/75896662.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定