A simple tkinter window
#coding=utf-8
# 模块导入
import tkinter as tk
if __name__=='__main__':
# 实例化窗口
window = tk.Tk()
# 给窗口起名字
window.title('My Window')
# 设定窗口的大小(长 乘 宽)
window.geometry('500x300')
#放入控件label
tk.Label(window, text='一个简单的窗口',bg='red').pack()
# 窗口循环显示,不断监听控件事件
window.mainloop()
How to display in full screen
There are many methods, here is one that is easier to use:
#coding=utf-8
# 模块导入
import tkinter as tk
# 主程序代码
if __name__=="__main__":
root = tk.Tk()
root.state("zoomed")
root.title('窗口')
root.mainloop()
Main window parameters
grammar | effect |
---|---|
window= tk.TK() | Create window |
window['height'] = 300 | Set high |
window['width'] = 500 | Set width |
window.title('Cube Station') | Set title |
window['bg'] = '#0099ff' | Set background color |
window.geometry("500x300+120+100") | Set the window size, +120 refers to the distance between the window and the left screen |
window.option_add('*Font', 'Fira 10') | Set global font |
window.resizable(width=False,height=True) | root.resizable(0,1) | Disable window resizing |
window.minsize(300,600) | Adjustable minimum value of the window |
window.maxsize(600,1200) | Maximum adjustable window |
window.attributes("-toolwindow", 1) | Toolbar style |
window.attributes("-topmost", -1) | Sticky window |
window.state("zoomed") | Window maximized |
window.iconify() | Minimize window |
window.deiconify() | Restore window |
window.attributes("-alpha",1) | The window is transparent, the transparency ranges from 0-1, 1 is opaque, 0 is fully transparent |
window.destroy() | close the window |
window.iconbitmap("./image/icon.ico") | Set window icon |
screenWidth = window.winfo_screenwidth() screenHeight = window.winfo_screenheight() |
Get screen width and height |
window.protocol("WM_DELETE_WINDOW", call) | When the window is closed, execute the call function |
window.mainloop () | The main window is updated cyclically |
Description of window attributes parameters:
parameter | effect |
---|---|
alpha | 1. (Windows, Mac) control the transparency of the window 2. 1.0 means opaque, 0.0 means completely transparent 3. This option does not support all systems, for systems that do not support, Tkinter draws an opaque (1.0) window |
disabled | (Windows) Disable the entire window (you can only close it from the task manager at this time) |
fullscreen | (Windows, Mac) If set to True, the window will be displayed in full screen |
modified | (Mac) If set to True, the window is marked as changed |
titlepath | (Mac) Set the path of the window proxy icon |
toolwindow | (Windows) If set to True, the window adopts the style of a tool window |
topmost | (Windows, Mac) If set to True, the window will always be placed on top |