Tkinter window properties
The following methods are used to set the Tkinter window properties related to windows.
Method | Description |
---|---|
title() |
set the title of the window |
geometry("widthxheight+x+y") |
Set the window width and height in Pixel, and set the window position. |
maxsize(width, height) |
Set the maximum width and height of the window when dragging and dropping |
minsize(width, height) |
Set the minimum width and height of the window when dragging and dropping |
configure(bg = "color") |
Set the background color of the window |
resizable(True, True) |
If you want to fix the width and height of the window, you can use resizable(0, 0) . |
state("zoomed") |
Maximize Window |
iconify |
Minimize window |
iconbitmap("xx.ico") |
Change the default window icon |
Example#1
Set the window title to ApidemosWindow, and also set the width to 300 and height to 160.
'''
Set the properties of the window
'''
from tkinter import *
# import tkinter
root = Tk() # Create root window Customized Tk object name, can also take other names
root.title("ApidemosWindow") # Title of the window
root.geometry("300x160") # Size of the window
root.configure(bg='yellow') # Background color of the window
# root.configure(bg='Olive') # Background color of the window
# root.configure(bg='#808000') # Background color of the window
# root.iconbitmap("1.jpg")
root.mainloop() # Let the program continue to run while entering the wait and handle window event, placed on the last line of the program
Output:
The above line 7 I set the window background color using bg. In addition to setting the color directly using the name, you can also set the color RGB using the hexadecimal method, where each color is represented by two hexadecimal digits.
Example#2
Use apidemos.com.ico to change the system default icon, while using another method to change the background color.
'''
Set the properties of the window
'''
from tkinter import *
# import tkinter
root = Tk() # Create root window Customized Tk object name, can also take other names
root.title("ApidemosWindow") # Title of the window
root.geometry("300x160") # Size of the window
root.configure(bg='#00ff00') # Background color of window
# root.configure(bg='Olive') # Background color of window
# root.configure(bg='#808000') # Background color of window
# root.iconbitmap("apidemos.com.ico")
# root.iconbitmap("./apidemos.com.ico") # Normal display
# root.iconphoto(False, PhotoImage(file='1.png')) # Can display png images normally
# root.iconphoto(False, PhotoImage(file='1.jpg')) # Program error reported: _tkinter.TclError: couldn't recognize data in image file "1.jpg"
root.mainloop() # Let the program continue to run while entering the wait and handle window event, placed on the last line of the program
Output: