建立一個自動最大化的 tkinter 視窗


有兩種不同的方法可以在 Tkinter 中獲得一個自動最大化的視窗。

  • 我們可以使用 Tkinter 的 state() 方法,並使用屬性 “zoomed” 呼叫它。
root.state("zoomed")
  • 第二種方法是使用 Tkinter 的 attributes 方法,引數為 “-fullscreen”,並將其設定為 True

預設情況下,Tkinter 建立了一個預定義大小的視窗。可以使用 geometry 方法來定製視窗的尺寸。例如:

root.geometry("700 x 350")

示例 1

# Import the required libraries
from tkinter import *

# Create an instance of tkinter frame
root=Tk()

# Create a label
Label(root, text="Welcome to Tutorialspoint", font="Calibri, 20").pack(pady=20)

# Maximize the window Size using state property
root.state('zoomed')

root.mainloop()

輸出

它將產生以下輸出 -

示例 2

現在,我們修改一下程式碼,使用 attribute 方法代替 state 方法。

# Import the required libraries
from tkinter import *

# Create an instance of tkinter frame
root=Tk()

# Create a label
Label(root, text="Welcome to Tutorialspoint", font="Calibri, 20").pack(pady=20)

# Maximize the window Size using attributes method
root.attributes('-fullscreen', True)

root.mainloop()

輸出

它將產生以下輸出 -

更新於:26-2021 年 10 月

2K+ 瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

入門
廣告