如何更改tkinter中按钮的图像
我正在尝试为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论