In my code, the second implementation correctly shows "some_img.png" as a button background but the first does not.
class QuizInterface:
def __init__(self):
self.window = Tk()
self.window.title("Quizzler")
self.window.config(bg=THEME_COLOR, padx=20, pady=20)
# Example 1: Works as expected
true_image = PhotoImage(file="./images/true.png")
self.true_button = Button(image=true_image)
self.true_button.grid(row=2, column=0)
# Example 2: Does not work as expected
self.true_button = Button(image=PhotoImage(file="./images/true.png"))
self.true_button.grid(row=2, column=0)
self.window.mainloop()
# QuizInterface object is created and called in my main.py with no error.
No error is thrown for the first example which is confusing. Additionally, I've noticed that I cannot define an object and then in the same line call .grid(..)
on that object without a "function does not return anything" warning. It seems as though tkinter does not like:
- defining multiple objects in a single line
- pack()/grid()/place() 'ing, in the same line as object construction
Why?