我目前正在尝试学习如何用tkinter构建gui,我的测试应用遇到了问题。在
我有一个按钮,它显示一个图像而不是文本,我也有一组单选按钮,我想控制哪些图像显示在常规按钮上。目前,单选按钮似乎没有更新我的photofilepath
StringVar,因为无论选中了哪个单选按钮,该按钮始终具有默认照片。以下是我的(简化)代码:
root = Tk() # Set up
root.title("Test GUI")
gui.grid(column=0, row=0, sticky=(N, W, E, S))
photofilepath = StringVar() # Set default photo
photofilepath.set("C:/Users/Ben/Pictures/Default photo.png")
photo = PhotoImage(file=photofilepath.get())
CalcButton = ttk.Button(gui, image=photo)
CalcButton.grid(column=3, row=2, columnspan=1)
# Set button photo
Photo1Rbutton = ttk.Radiobutton(gui, text="Photo 1", variable=photofilepath,
value='C:/Users/Ben/Pictures/Photo 1.png')
Photo1Rbutton.grid(column=4, row=2, sticky=S)
Photo2Rbutton = ttk.Radiobutton(gui, text="Photo 2", variable=photofilepath,
value='C:/Users/Ben/Pictures/Photo 2.png')
Photo2Rbutton.grid(column=4, row=3)
root.mainloop()
提前谢谢你的帮助。在
您可以在
Radiobutton
中使用command=
来分配将加载新图像并将它们放入标签中的函数。在工作示例(只需设置路径)
相关问题 更多 >
编程相关推荐