【问题标题】:image on button using Tkinter python使用 Tkinter python 在按钮上的图像
【发布时间】:2019-03-20 17:38:33
【问题描述】:

我正在尝试使用 Tkinter gui 将图像放在按钮上,但按钮不是照片,而是白色方块。 我使用这些行:

button = tk.Button(self.root, height=100, width=165, 
                   text=name_to_teach_command, command=action_with_args)
st = ImageTk.PhotoImage(file=photo_location)
button.config(image=st)
button.configure(relief=FLAT)

感谢您的帮助!

【问题讨论】:

  • 这是因为局部变量st 用于保存一旦函数退出将被销毁的图像。这种问题已经回答了几十次了。
  • @acw1668 已经在stackoverflow.com/questions/54815388/…回复了一个几乎完全相同的副本

标签: python tkinter


【解决方案1】:

您面临的问题可能是由于图像的大小。Tkinter 不会使图像适合空间,您必须调整其大小。 您还可以对代码进行一些更改:

st = ImageTk.PhotoImage(file=photo_location)
button = tk.Button(self.root, height=100, width=165, 
                   image= st, command=action_with_args)
button.configure(relief=FLAT)

希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-02
    • 2014-07-12
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 2015-08-03
    • 2019-01-20
    相关资源
    最近更新 更多