如何在 python 3.7 中的 Tk canvas 中创建按钮
How to create a button inside of a a Tk canvas in python 3.7
我想知道如何使用 tkinter 模块将按钮放入 canvas 中。有人问过这个问题,但那是 5 年前的事了,而且版本不同,所以对我的情况来说不是很方便,而且我还是个初学者,所以我只理解了最佳答案中大约 3/4 的代码。这是问题:How to make a Button using the tkinter Canvas widget?
from tkinter import *
root = Tk()
c=Canvas(root,width=200,height=150,bg='blue')
c.pack(side = 'top')
c.create_text(100,50,anchor='c',fill='orange',font='Times 28',text='List')
button = Button(root, text="Quit",command=root.destroy)
button.pack()
mainloop()
当我 运行 这段代码时,它会在我的 Canvas 下面创建按钮,而不是 Canvas。我在 https://docs.python.org/3.7/library/tkinter.html 我正在使用的 IDE 指南上寻求帮助。我找不到将按钮放在 Canvas 上的方法,即使我可能会或可能不会错过某些东西。如果这个问题被认为没有帮助或不必要,我深表歉意并会立即将其关闭。
Python版本:3.7
级别:初级
运行 代码: IDLE 64 位
OS: Windows 10
当你使用 pack()
时,tkinter 会将按钮放在它的 master(根)上,并且绘制 canvas 的区域已经被占用。
要将按钮放在 canvas 上,您应该在 canvas 上使用函数 create_window()
:
from tkinter import *
root = Tk()
c = Canvas(root,width=200,height=150,bg='blue')
c.pack(side = 'top')
c.create_text(100, 50, anchor='c', fill='orange', font='Times 28', text='List')
button = Button(root, text="Quit", command=root.destroy)
canvas_widget = c.create_window(100, 100, window=button)
root.mainloop()
删除button.pack()
尝试使用下面的代码
button = Button(root, text="Quit", command=root.destroy)
c.create_window(10, 10, 锚点=NW, window=按钮)
我想知道如何使用 tkinter 模块将按钮放入 canvas 中。有人问过这个问题,但那是 5 年前的事了,而且版本不同,所以对我的情况来说不是很方便,而且我还是个初学者,所以我只理解了最佳答案中大约 3/4 的代码。这是问题:How to make a Button using the tkinter Canvas widget?
from tkinter import *
root = Tk()
c=Canvas(root,width=200,height=150,bg='blue')
c.pack(side = 'top')
c.create_text(100,50,anchor='c',fill='orange',font='Times 28',text='List')
button = Button(root, text="Quit",command=root.destroy)
button.pack()
mainloop()
当我 运行 这段代码时,它会在我的 Canvas 下面创建按钮,而不是 Canvas。我在 https://docs.python.org/3.7/library/tkinter.html 我正在使用的 IDE 指南上寻求帮助。我找不到将按钮放在 Canvas 上的方法,即使我可能会或可能不会错过某些东西。如果这个问题被认为没有帮助或不必要,我深表歉意并会立即将其关闭。
Python版本:3.7
级别:初级
运行 代码: IDLE 64 位
OS: Windows 10
当你使用 pack()
时,tkinter 会将按钮放在它的 master(根)上,并且绘制 canvas 的区域已经被占用。
要将按钮放在 canvas 上,您应该在 canvas 上使用函数 create_window()
:
from tkinter import *
root = Tk()
c = Canvas(root,width=200,height=150,bg='blue')
c.pack(side = 'top')
c.create_text(100, 50, anchor='c', fill='orange', font='Times 28', text='List')
button = Button(root, text="Quit", command=root.destroy)
canvas_widget = c.create_window(100, 100, window=button)
root.mainloop()
删除button.pack()
尝试使用下面的代码
button = Button(root, text="Quit", command=root.destroy)
c.create_window(10, 10, 锚点=NW, window=按钮)