📅  最后修改于: 2023-12-03 15:04:55.519000             🧑  作者: Mango
When it comes to building desktop applications, Python's Tkinter library offers a user-friendly solution. Tkinter has a wide range of widgets, including buttons, labels, and entry boxes, which make GUI building an easy job. One essential feature of any GUI is the background color. The bg
or background
attribute helps define the color for the window background, and root
represents the top-level window.
Here's how to create a simple GUI with a red background:
import tkinter as tk
root = tk.Tk()
root.configure(background='red')
root.geometry("300x300")
root.mainloop()
In this code, we have created an instance of Tkinter
class Tk()
and then configured the background color using configure()
. Also, geometry()
method is used to fix the dimensions of the window, and mainloop()
function is used to keep the window running until the user closes the application.
Apart from the background, you can customize other aspects of the window, such as the title, icon, and window size. Here's an example:
import tkinter as tk
root = tk.Tk()
root.title("Hello Tkinter!")
root.iconbitmap('icon.ico')
root.geometry("300x200")
root.configure(background='light blue')
root.mainloop()
In this example, title()
sets the title of the window, while iconbitmap()
sets the icon in .ico
format. Then, geometry()
sets the size of the window to 300x200
, and configure()
sets the background color. Finally, mainloop()
keeps the application running.
Overall, using Tkinter with Python to build GUI desktop applications is an easy task, and the root
and bg
attributes help you customize your application's appearance.