如何更改tkinter中按钮的图像

发布于 2025-01-25 12:48:57 字数 1737 浏览 4 评论 0原文

我正在尝试为TKINTER中的游戏编程按钮,以随机更改图片。但是,一方面,我不知道如何随机化按钮,以使随机按钮更改其布局,然后按钮如何更改其布局。我尝试了不同的事情,最后尝试是.config()。希望您能帮助我!谢谢Max,

from tkinter import *
import random
# Create Object
root = Tk()

score = 0

#first image
photo1 = PhotoImage(file = r"C:/Users/m_buchner/Pictures/180px-U+25CF.svg.png")

#buttons
img0 = Button(root, image = photo1,
            borderwidth = 0)
img0.grid(row=1, column=1)
img1 = Button(root, image = photo1,
            borderwidth = 0)
img1.grid(row=1, column=2)
img2 = Button(root, image = photo1,
            borderwidth = 0)
img2.grid(row=1, column=3)
img3 = Button(root, image = photo1,
            borderwidth = 0)
img3.grid(row=2, column=1)
img4 = Button(root, image = photo1,
            borderwidth = 0)
img4.grid(row=2, column=2)
img5 = Button(root, image = photo1,
            borderwidth = 0)
img5.grid(row=2, column=3)

label = Label(root, width=20, height=2, text=score)
label.grid(row=0, column=2)

label = Label(root, width=20, height=2, text=score)
label.grid(row=0, column=2)




root.mainloop()

def change_button():
    
    photo1 = PhotoImage(file = r"C:/Users/m_buchner/Pictures/180px-U+25CF.svg.png")
    photo2 = PhotoImage(file = r"C:/Users/m_buchner/Pictures/Screenshot 2022-05-02 195231.png")
    
    img0.configure(root, image = photo2,
                borderwidth = 0)

    img1.configure(root, image = photo1,
                borderwidth = 0)

    img2.configure(root, image = photo1,
                borderwidth = 0)

    img3.configure(root, image = photo1,
                borderwidth = 0)

    img4.configure(root, image = photo1,
                borderwidth = 0)

    img5.configure(root, image = photo1,
                borderwidth = 0)

非常感谢您的帮助! ❤

I'm trying to program buttons for a game in tkinter, which change the picture randomly. However, on the one hand I don't know how to randomize the buttons so that a random button changes its layout, and then how the button changes its layout. I've tried different things and my last attempt was .config(). I hope you can help me! Thank you Max

from tkinter import *
import random
# Create Object
root = Tk()

score = 0

#first image
photo1 = PhotoImage(file = r"C:/Users/m_buchner/Pictures/180px-U+25CF.svg.png")

#buttons
img0 = Button(root, image = photo1,
            borderwidth = 0)
img0.grid(row=1, column=1)
img1 = Button(root, image = photo1,
            borderwidth = 0)
img1.grid(row=1, column=2)
img2 = Button(root, image = photo1,
            borderwidth = 0)
img2.grid(row=1, column=3)
img3 = Button(root, image = photo1,
            borderwidth = 0)
img3.grid(row=2, column=1)
img4 = Button(root, image = photo1,
            borderwidth = 0)
img4.grid(row=2, column=2)
img5 = Button(root, image = photo1,
            borderwidth = 0)
img5.grid(row=2, column=3)

label = Label(root, width=20, height=2, text=score)
label.grid(row=0, column=2)

label = Label(root, width=20, height=2, text=score)
label.grid(row=0, column=2)




root.mainloop()

def change_button():
    
    photo1 = PhotoImage(file = r"C:/Users/m_buchner/Pictures/180px-U+25CF.svg.png")
    photo2 = PhotoImage(file = r"C:/Users/m_buchner/Pictures/Screenshot 2022-05-02 195231.png")
    
    img0.configure(root, image = photo2,
                borderwidth = 0)

    img1.configure(root, image = photo1,
                borderwidth = 0)

    img2.configure(root, image = photo1,
                borderwidth = 0)

    img3.configure(root, image = photo1,
                borderwidth = 0)

    img4.configure(root, image = photo1,
                borderwidth = 0)

    img5.configure(root, image = photo1,
                borderwidth = 0)

Thank you very much for your help! ❤

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文