Python Tkinter:窗口樣式設置


Tkinter 之主窗口參數

一、常用參數

語法 作用
window= tk.TK() 創建窗口
window['height'] = 300 設置高
window['width'] = 500 設置寬
window.title('魔方小站') 設置標題
window['bg'] = '#0099ff' 設置背景色
window.geometry("500x300+120+100") 設置窗口大小,+120指窗口距離左屏幕的距離
window.option_add('*Font', 'Fira 10') 設置全局字體
window.resizable(width=False,height=True) | root.resizable(0,1) 禁止窗口調整大小
window.minsize(300,600) 窗口可調整的最小值
window.maxsize(600,1200) 窗口可調整的最大值
window.attributes("-toolwindow", 1) 工具欄樣式
window.attributes("-topmost", -1) 置頂窗口
window.state("zoomed") 窗口最大化
window.iconify() 窗口最小化
window.deiconify() 還原窗口
window.attributes("-alpha",1) 窗口透明化,透明度從 0-1,1 是不透明,0 是全透明
window.destroy() 關閉窗口
window.iconbitmap("./image/icon.ico") 設置窗口圖標
screenWidth = window.winfo_screenwidth()
screenHeight = window.winfo_screenheight()
 獲取屏幕寬高
window.protocol("WM_DELETE_WINDOW", call) 當窗口關閉時,執行call函數
window.mainloop() 主窗口循環更新

窗口attributes參數說明:

參數 作用
alpha  1.(Windows,Mac)控制窗口的透明度
2. 1.0 表示不透明,0.0 表示完全透明
3. 該選項並不支持所有的系統,對於不支持的系統,Tkinter 繪制一個不透明(1.0)的窗口
disabled  (Windows)禁用整個窗口(這時候你只能從任務管理器中關閉它)
fullscreen  (Windows,Mac)如果設置為 True,則全屏顯示窗口
modified  (Mac)如果設置為 True,該窗口被標記為改動過
titlepath  (Mac)設置窗口代理圖標的路徑
toolwindow   (Windows)如果設置為 True,該窗口采用工具窗口的樣式
topmost  (Windows,Mac)如果設置為 True,該窗口將永遠置於頂層

 

 

 

獲得窗口的寬度和高度

 
        
import tkinter
win = tkinter.Tk()
win.geometry("100x100")
win.update()
print("當前窗口的寬度為",win.winfo_width())
print("當前窗口的高度為",win.winfo_height())
win.mainloop()

 

窗口居中

 
        
#-*- coding:utf-8 -*-
from tkinter import *
win = Tk()
sd = win.winfo_screenwidth() #得到屏幕寬度
sh = win.winfo_screenheight() #得到屏幕高度
wd = 450
wh = 300 
x = (sd-wd) / 2 #居中
y = (sh-wh) / 2 #居中
win.geometry("%dx%d+%d+%d" %(wd,wh,x,y))
mainloop()
 
        

 

 

二、代碼示例

 

 

import tkinter as tk
 
# 創建窗體
window = tk.Tk()
 
def call():
    global window
    window.destroy()
 
def main():
    global window
    # 設置主窗體大小
    winWidth = 600
    winHeight = 400
    # 獲取屏幕分辨率
    screenWidth = window.winfo_screenwidth()
    screenHeight = window.winfo_screenheight()
    # 計算主窗口在屏幕上的坐標
    x = int((screenWidth - winWidth)/ 2)
    y = int((screenHeight - winHeight) / 2)
     
    # 設置主窗口標題
    window.title("主窗體參數說明")
    # 設置主窗口大小
    window.geometry("%sx%s+%s+%s" % (winWidth, winHeight, x, y))
    # 設置窗口寬高固定
    window.resizable(0,0)
    # 設置窗口圖標
    window.iconbitmap("./image/icon.ico")
    # 設置窗口頂部樣式
    window.attributes("-toolwindow", 0)
    # 設置窗口透明度
    window.attributes("-alpha",1)
    #獲取當前窗口狀態
    print(window.state())
     
    window.protocol("WM_DELETE_WINDOW", call)
     
    #循環更新
    window.mainloop()
 
 
 
if __name__ == "__main__":
    main()

 

 

import tkinter as tk #導入模塊 命名為 tk

# 創建窗口

window = tk.Tk()

# 調整窗口大小和位置,單位是像素

width, height = 500, 300

place_x, place_y = 100, 100 #位置以屏幕左上角為起始點(0,0)

window.geometry(f'{width}x{height}+{place_x}+{place_y}')

# 設置窗口標題

window.title('My window')

# 設置窗口背景顏色

window.config(bg='black')

# 窗口創建后一定時間執行程序

def config_() : #定義一個打印字符串 ‘Hello world’ 的函數
    print("Hello world")

window.config(bg='white')

window.after(2000, config_) #第一個參數是時間,單位是毫秒;第二個參數是要執行的程序,注意,不用加括號

# 隱藏窗口外部

window.overrideredirect(True)

# 固定窗口尺寸

window.resizable(0, 0)

# 給窗口添加圖標

#icon = tk.PhotoImage(file = '/pilogo.png')

#window.iconphoto(True, icon)

# 設置窗口最小|最大尺寸

window.minsize(150, 100) #最小尺寸

window.maxsize(500, 300) #最大尺寸

# 窗口創建4秒后關閉窗口

window.after(4000, window.destroy)

# 窗口全屏

#window.attributes('-fullscreen', True)

# 窗口置頂

window.attributes('-topmost', True)


window.mainloop()

 

 

#Import the required libraries from tkinter import * #Create an instance of Tkinter Frame win = Tk() #Set the geometry win.geometry("700x350") #Set the default color of the window win.config(bg='#aad5df') #Create a Label to display the text label=Label(win, text= "Hello World!",font= ('Helvetica 18 bold'), background= 'white', foreground='purple1') label.pack(pady = 50) win.update() #Return and print the width of label widget width = label.winfo_width() print("The width of the label is:", width, "pixels") win.mainloop()

 

 

How do I get a windows current size using Tkinter?

Use the following universal widget methods (where w is a widget):

w.winfo_height()
w.winfo_width()

You can also use the following:

w.winfo_reqheight()
w.winfo_reqwidth()

Read about universal widget methods.

 

 

 

 

REF

https://www.cnblogs.com/yang-2018/p/11781535.html

https://stackoverflow.com/questions/4065783/how-do-i-get-a-windows-current-size-using-tkinter

https://blog.csdn.net/weixin_39932300/article/details/111431678


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM