如何使python tkinter背景颜色可以根据不同情况改变
时间: 2023-07-27 22:30:23 浏览: 157
你可以在Tkinter中使用`configure`方法来改变背景颜色。具体的方法是,先创建一个Frame或者Canvas组件,然后使用`config`方法来改变它的背景颜色。例如:
```python
import tkinter as tk
root = tk.Tk()
frame = tk.Frame(root, bg='white')
frame.pack(fill='both', expand=True)
def change_color(color):
frame.config(bg=color)
button1 = tk.Button(root, text='Red', command=lambda: change_color('red'))
button1.pack(side='left')
button2 = tk.Button(root, text='Green', command=lambda: change_color('green'))
button2.pack(side='left')
button3 = tk.Button(root, text='Blue', command=lambda: change_color('blue'))
button3.pack(side='left')
root.mainloop()
```
这个例子中,我们创建了一个白色的Frame,并在窗口中添加了三个按钮,分别对应红、绿、蓝三种颜色。当我们点击按钮时,就会调用`change_color`函数,然后通过`config`方法来改变Frame的背景颜色。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)