[英]How to make button using canvas in toplevel() Tkinter
I'm trying to make a button using canvas.create_window
in toplevel()
in Tkinter.我正在尝试在 Tkinter 的
toplevel()
中使用canvas.create_window
制作一个按钮。 Button is used to go back to main window.按钮用于 go 回到主 window。 First "Start" button is displayed but second "Back" button is not.
显示第一个“开始”按钮,但不显示第二个“返回”按钮。 Code below.
代码如下。
from tkinter import *
win = Tk()
def play_button():
win.withdraw()
top = Toplevel()
top.geometry("300x300")
button_back = Button(top, text="Back", command=back_button)
canvas_two = Canvas(top, width = 300, height = 300)
canvas_two.pack(fill="both", expand=True)
Button_reverse = canvas_two.create_window(0, 0, anchor="nw", window=button_back)
top.resizable(False, False)
def back_button():
win.deiconify()
win.geometry("300x300")
canvas = Canvas(win, width = 300, height = 300)
canvas.pack(fill="both", expand=True)
button_play = Button(win, text="Play", command=play_button)
Play_button = canvas.create_window(0, 0, anchor="nw", window=button_play )
win.mainloop()
The problem is the ordering of creation of canvas_two
and button_back
.问题是
canvas_two
和button_back
的创建顺序。 You need to create the Canvas
first and then put the Button
on top of it as shown below.您需要先创建
Canvas
,然后将Button
放在上面,如下所示。
def play_button():
win.withdraw()
top = Toplevel()
top.geometry("300x300")
canvas_two = Canvas(top, width=300, height=300)
canvas_two.pack(fill="both", expand=True)
button_back = Button(top, text="Back", command=back_button)
Button_reverse = canvas_two.create_window(0, 0, anchor="nw", window=button_back)
top.resizable(False, False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.