如何在 Canvas 上放置按钮?
How do I put a button on a Canvas?
所以我可以在简单的 window 中做一个按钮,但不能在 Canvas 中做一个按钮。
from tkinter import *
window = Tk()
def function():
print('Hello World')
tk_button = Button(window, text = 'Click me!', command = function)
我想将按钮放置在 tkinter Canvas 上并在其周围放置图形,而不仅仅是带有按钮的 window。
您可以使用 create_window 函数在 canvas 上放置一个框架,然后使用该框架正常打包其他小部件。
import tkinter as tk
mw = tk.Tk()
canvas = tk.Canvas(mw, bg='grey75')
canvas.pack()
frame = tk.Frame(canvas, width=50, height=5)
canvas.create_window((1,1), window=frame, anchor='nw')
button = tk.Button(frame, text='Hello World')
button.pack()
mw.mainloop()
所以我可以在简单的 window 中做一个按钮,但不能在 Canvas 中做一个按钮。
from tkinter import *
window = Tk()
def function():
print('Hello World')
tk_button = Button(window, text = 'Click me!', command = function)
我想将按钮放置在 tkinter Canvas 上并在其周围放置图形,而不仅仅是带有按钮的 window。
您可以使用 create_window 函数在 canvas 上放置一个框架,然后使用该框架正常打包其他小部件。
import tkinter as tk
mw = tk.Tk()
canvas = tk.Canvas(mw, bg='grey75')
canvas.pack()
frame = tk.Frame(canvas, width=50, height=5)
canvas.create_window((1,1), window=frame, anchor='nw')
button = tk.Button(frame, text='Hello World')
button.pack()
mw.mainloop()