I looked at other answers to questions similar to mine, but nothing worked for some reason. I am trying to create a button after another button it's clicked in tkinter. My code looks like this:
def BookButton():
global book_button_image
book_button_image = PhotoImage(file=relative_to_books(f"History.png"))
book_button = Button(
image = book_button_image,
borderwidth=0,
highlightthickness=0,
command = lambda: print('PDF opened'),
relief="flat",
)
book_button.place(
x=850.0,
y=344.5,
width=370.0,
height=498.0
)
book_button.pack()
print('button created')
global button_image_1
button_image_1 = PhotoImage(
file=relative_to_assets("button_1.png"))
button_1 = Button(
image=button_image_1,
borderwidth=0,
highlightthickness=0,
command=lambda: [BookButton()],
relief="flat",
)
button_1.place(
x=850.0,
y=708.0,
width=286.0,
height=85.0
)
I see that the BookButton function gets called and executed, but the button doesn't show up. Does anyone know what am I doing wrong and what should I change?
It doesn't work because you are using place
to position the widget. So even if the function is executed multiple times. The same looking button will duplicate and stay there. It is just an illusion. There are actually many buttons but it looks like its only one because they are on top of each other. Just use the pack
or grid
method in tkinter. I hope this answers your question.
EDIT:
It also doesn't work because you did not specify the root window when creating a button. You should have to have something like this button = Button(root_window, other parameters....)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.