Change Python Canvas Background Color with Tkinter
In Python, the tkinter library can be used to create GUI applications, and the background color of the canvas can be changed by setting it. Here is a simple example code demonstrating how to set the canvas background color to red:
import tkinter as tk
# 创建一个窗口
window = tk.Tk()
# 创建一个画布并设置宽高
canvas = tk.Canvas(window, width=200, height=200)
# 设置画布的背景色为红色
canvas.configure(bg='red')
# 将画布放置在窗口中
canvas.pack()
# 运行窗口
window.mainloop()
In the code above, we first import the tkinter library, then create a window object. Next, we create a canvas object and set its width and height, then use the configure method to set the canvas background color to red. Finally, we place the canvas in the window and run the window using the mainloop method. Using a similar method, you can set the canvas background color to any color you like.