python中Tkinter詳細基礎(chǔ)教學實例代碼
前言
本文致力與幫助想要需要Tkinter的小伙伴,內(nèi)容詳細簡介明了不臃腫,會詳細的介紹Tkinter的常用操作以及核心組件和它們的重要參數(shù)。想要學習的小伙伴一定要認真觀看喔~
模塊導入
tk為python只帶的標準庫,不需要下載,直接導入。
from tkinter import *
tkinter核心組件
label | 標簽 | 用來顯示文字或圖片 |
---|---|---|
Button | 按鈕 | 類似標簽,但提供額外的功能,例如鼠標掠過、按下、釋放以及鍵盤操作、事件 |
Entry | 單行文字域 | 用來收集鍵盤輸入 |
Text | 多行文字區(qū)域 | 可用來收集(或顯示)用戶輸入的文字 |
Frame | 框架 | 包含其他組件的純?nèi)萜?/td> |
Checkbutton | 選擇按鈕 | 一組方框,可以選擇其中的任意個 |
Listbox | 列表框 | 一個選項列表,用戶可以從中選擇 |
Menu | 菜單 | 點下菜單按鈕后彈出的一個選項列表,用戶可以從中選擇 |
Menubutton | 菜單按鈕 | 用來包含菜單的組件(有下拉式、層疊式等等) |
Message | 消息框 | 類似于標簽,但可以顯示多行文本 |
Radiobutton | 單選按鈕 | 組按鈕,其中只有一個可被“按下” (類似 HTML 中的 radio) |
Scale | 進度條 | 線性“滑塊”組件,可設(shè)定起始值和結(jié)束值,會顯示當前位置的精確值 |
Scrollbar | 滾動條 | 對其支持的組件(文本域、畫布、列表框、文本框)提供滾動功能 |
Toplevel | 頂級 | 類似框架,但提供一個獨立的窗口容器 |
Canvas | 繪畫 | 提供繪圖功能(直線、橢圓、多邊形、矩形) 可以包含圖形或位圖 |
基礎(chǔ)架構(gòu)
第一步 導入tk模塊
from tkinter import *
第二步 獲取TK對象
root = Tk()
第三步 指定窗口大小位置
# 指定了窗口的寬度、高度和位置。 root.geometry('600x450+400+200') # 寬度為600像素 # 高度為450像素 # 位置在屏幕上的坐標為(x=400, y=200)
第四步 主窗口標題
# 主窗口的框體的標題 root.title('title')
第五步 顯示主窗口
root.mainloop() # 顯示主窗口
總和
from tkinter import * root = Tk() root.geometry('400x300+400+200') root.title('title') """ 此處寫tk框架組件 """ root.mainloop()
1、Label 標簽
基本屬性
參數(shù) | 注釋 |
---|---|
text | 標簽名稱 |
font | 字體(樣式,大?。?/td> |
bg (background) | 背景顏色(標簽顏色) |
fg (foreground) | 前景顏色(字體顏色) |
width | 標簽寬度 |
height | 標簽高度 |
anchor | 錨選項,控制標簽文本的位置(參數(shù)值:S,W,E,N,SE,SW,NW,NE,CENTER,默認為CENTER) |
bitmap | 位圖 |
relief | 三維效果(參數(shù)值:FLAT、SUNKEN、RAISED、GROOVE、RIDGE。默認為 FLAT) |
image | 與PhotoImage 一起使用,圖片只能為gif圖片 |
compound | 圖片和文字一同顯示 |
padx | 設(shè)置文本與標簽邊框x軸方向上距離 |
pady | 設(shè)置文本與標簽邊框y軸方向上的距離 |
cursor | 鼠標移動到框架時,光標的形狀(參數(shù)值:arrow, circle, cross, plus 等) |
justify | 顯示多行文本的時候,設(shè)置不同行之間的對齊方式(參數(shù)值:LEFT, RIGHT, CENTER) |
state | 設(shè)置標簽狀態(tài),參數(shù)值:NORMAL、ACTIVE、 DISABLED。默認 NORMAL |
wraplength | 指定每行文本的寬度,單位是屏幕單元 |
underline | 下劃線。默認按鈕上的文本都不帶下劃線。取值就是帶下劃線的字符串索引,為 0 時,第一個字符帶下劃線,為 1 時,第二個字符帶下劃線,以此類推 |
1.1 text,bg,font,fg
from tkinter import * root = Tk() root.geometry('400x300+400+200') root.title('title') # ------ label1 = Label(root, text='測試1', bg='purple', font=('華文行楷', 20), fg='blue') label1.grid(row=1, column=1) # ------ root.mainloop()
其中 grid(row=1,column=1) 代表著當前標簽位置于 第一行第一列
text為標簽名“測試1”
bg為底色“purple”紫色
font為字體“華文行楷”以及字體大小“20”
fg為字體顏色“bule”藍色
1.2 width,height,anchor,padx
from tkinter import * root = Tk() root.geometry('400x300+400+200') root.title('title') # ------ label2 = Label(root, text='測試2', bg='purple', font=('華文行楷', 20), fg='blue', width=8, height=3, anchor=E, padx=20) label2.grid(row=2, column=1) # ------ root.mainloop()
其中 grid(row=2,column=1) 代表著當前標簽位置于 第二行 第一列。而因為前面沒有內(nèi)容行,因此標簽顯示在第一行。
width為寬度“8”
height為高度"3"
anchor=E為文本在標簽中的位置
padx=20為文本與標簽邊框x軸方向上距離20
1.3 image,compound,relief
from tkinter import * root = Tk() root.geometry('400x300+400+200') root.title('title') # ------ preview = PhotoImage(file=r'Aa_圖片素材庫/preview.gif') label3 = Label(root, text='圖片', image=preview, compound='left', relief=SUNKEN) label3.grid(row=2, column=2) # ------ root.mainloop()
PhotoImage(file="....")中的file為gif圖片文件位置
image與PhotoImage 一起使用,為標簽顯示當前圖片,且圖片只能為gif圖片
compound為表示圖片和文字一同顯示,left表示圖片顯示在標簽左邊
relief為三維效果,且SUNKEN為顯示方式
1.4 bitmap,bd
from tkinter import * root = Tk() root.geometry('400x300+400+200') root.title('title') # ------ label4 = Label(root, bitmap='error', bd=3, relief=SUNKEN) label4.grid(row=2, column=4) label5 = Label(root, relief=SUNKEN, text='bitmap位圖測試') label5.grid(row=2, column=3) # ------ root.mainloop()
bd為
指定標簽部件的邊框?qū)挾?,這里設(shè)置為3像素
bitmap
用于指定在標簽部件中顯示的位圖。'error'表示顯示一個帶有問題圖標的位圖。它們有如下'error'、'info'、'question'等,分別代表錯誤、信息和問題圖標。也可以自定義的位圖文件。
2、Button 按鈕
基本屬性
參數(shù) | 注釋 |
---|---|
text | 按鈕文本內(nèi)容 |
font | 字體(樣式,大小) |
bg | 背景顏色(按鈕顏色) |
fg | 前景顏色(字體顏色) |
width | 按鈕寬度 |
height | 按鈕高度 |
command | 按鈕關(guān)聯(lián)的函數(shù),當按鈕被點擊時,執(zhí)行該函數(shù) |
padx | 設(shè)置按鈕文本與按鈕邊框x軸方向的距離 |
pady | 設(shè)置按鈕文本與按鈕邊框y軸方向的距離 |
bd | 按鈕邊框?qū)挾?/td> |
anchor | 控制按鈕文本的位置(參數(shù)值:S,W,E,N,SE,SW,NW,NE,CENTER,默認為CENTER) |
image | 與PhotoImage 一起使用,圖片只能為gif格式 |
relief | 三維效果 (參數(shù)值:FLAT、SUNKEN、RAISED、GROOVE、RIDGE。默認為 FLAT) |
bitmap | 位圖 |
compound | 圖片和文字一同顯示 |
cursor | 鼠標移動到框架時,光標的形狀(參數(shù)值:arrow, circle, cross, plus 等) |
justify | 顯示多行文本的時候,設(shè)置不同行之間的對齊方式(參數(shù)值:LEFT, RIGHT, CENTER) |
state | 設(shè)置按鈕狀態(tài),參數(shù)值:NORMAL、ACTIVE、 DISABLED。默認 NORMAL |
wraplength | 指定每行文本的寬度,單位是屏幕單元 |
underline | 下劃線。默認按鈕上的文本都不帶下劃線。取值就是帶下劃線的字符串索引,為 0 時,第一個字符帶下劃線,為 1 時,第二個字符帶下劃線,以此類推 |
2.1 text,font,bg,fg,width,height
from tkinter import * root = Tk() root.geometry('400x300+400+200') root.title('title') button1 = Button(root, text='測試1', bg='purple', font=('華文行楷', 20), fg='blue', width=8, height=2) button1.grid(row=2, column=1) root.mainloop()
這些屬性功能基本與標簽屬性類型,分別為按鈕名,文字樣式,底色,按鈕文字顏色,寬和高。
2.2 command,padx,pady,bd,anchor
from tkinter import * from tkinter import messagebox root = Tk() root.geometry('400x300+400+200') def test(): messagebox.showinfo('提示', '已點擊 測試1 按鈕') button1 = Button(root, text='測試1', height=3, width=8, padx=20, pady=30, command=test, bd=2, anchor=E) button1.grid(row=1, column=1) root.mainloop()
"command=test" 意思是當點擊此按鈕時觸發(fā)函數(shù)test,其中test函數(shù)意義為顯示出一個彈窗。是tk中重要的按鈕屬性
padx為按鈕內(nèi)文字與按鈕邊框x軸的距離
pady為按鈕內(nèi)文字與按鈕邊框y軸的距離
bd為按鈕邊框?qū)挾?/p>
"anchor=E"表示按鈕上的文本將沿著按鈕的右側(cè)對齊,其中默認為CENTER居中
2.3 image,relief,bitmap,compound,cursor
from tkinter import * root = Tk() root.geometry('400x300+400+200') button1 = Button(root, text='測試', cursor='cross', relief=SUNKEN, bitmap='question',) button1.grid(row=1, column=2) picture = PhotoImage(file=r'Aa_圖片素材庫/3f12a.gif') button2 = Button(root, text='圖片', width=100, height=100, image=picture, compound='left') button2.grid(row=2, column=2) root.mainloop()
image是為將按鈕的樣子變?yōu)楫斍癵if動態(tài)圖
relief將當前按鈕以三維效果顯示,此代碼意思是將問號按鈕沉沒顯示,默認為平。
bitmap設(shè)置按鈕的位圖。位圖是一種小的圖像,通常用于表示簡單的圖標或符號。將位圖文件的路徑傳遞給bitmap
參數(shù)?;蚴褂胻k自帶的位圖。
cursor則表示鼠標放當前按鈕上時顯示樣式
2.4 justify,state,wraplength,underline
from tkinter import * root = Tk() root.geometry('400x300+400+200') button1 = Button(root, text='測試多行文字展示多行文字展示', underline=0, state=NORMAL, justify=RIGHT, wraplength=60, height=3, width=10) button1.grid(row=1, column=1) root.mainloop()
justify=RIGHT:讓多行文字靠右顯示
state=NORMAL:按鈕狀態(tài),NORMAL為正常狀態(tài)也是默認狀態(tài):
warplength=60:表示么行文本寬度為100(單位屏幕單元)
underlinr=0:表示按鈕第一個文字帶下劃線
3、Enter單行文字域
基本屬性
參數(shù) | 注釋 |
---|---|
bg | 背景色 |
fg | 底色 |
font | 字體(樣式,大?。?/td> |
width | 文本框?qū)挾?/td> |
bd | 文本框框?qū)?/td> |
show | 輸入顯示方式 |
textvariable | 將一個StringVar 對象與Entry 小部件關(guān)聯(lián)起來,以便在用戶輸入文本時實時更新 |
3.1 show,textvariable
from tkinter import * root = Tk() root.geometry('500x500+400+200') # 創(chuàng)建一個StringVar對象 text_var = StringVar() # 將StringVar對象與Entry小部件關(guān)聯(lián) entry = Entry(root, textvariable=text_var, width=30, show="*") entry.pack() def get_text(): text = text_var.get() print("用戶輸入的文本是:", text) button = Button(root, text="獲取文本", command=get_text) button.pack() root.mainloop()
show=“*” :將文本框顯示的內(nèi)容以‘*’顯示
textvariable:關(guān)聯(lián),方便使用
3.2 方法展示:get()
from tkinter import * root = Tk() root.geometry('500x500+400+200') entry = Entry(root) entry.pack() def get_text(): text = entry.get() print("Entry中的文本是:", text) button = Button(root, text="獲取文本", command=get_text) button.pack() root.mainloop()
gntry.get():獲取entry輸入的文本內(nèi)容并返回
3.3 方法展示:insert()
from tkinter import * root = Tk() root.geometry('500x500+400+200') root.title('胖兔always') entry = Entry(root) entry.pack() def insert_text(): entry.insert(0, "Hello, World!") button = Button(root, text="插入文本", command=insert_text) button.pack() root.mainloop()
entry.insert() :表示從文本起始位置插入文本到文本域中
3.4 方法展示:delete()
from tkinter import * root = Tk() root.geometry('500x500+400+200') root.title('胖兔always') entry = Entry(root) entry.pack() def delete_text(): entry.delete(0, END) button = Button(root, text="刪除文本", command=delete_text) button.pack() root.mainloop()
entry.delete(0, END) : 從開頭到末尾的所有文本內(nèi)容。這個方法調(diào)用會清空Entry
小部件中的所有文本內(nèi)容,使其變?yōu)榭瞻谞顟B(tài)。
3.5 方法展示:config()
from tkinter import * root = Tk() root.geometry('500x500+400+200') root.title('胖兔always') entry = Entry(root) entry.pack() def configure_entry(): entry.config(bg="yellow", fg="blue", font=("Arial", 12)) button = Button(root, text="配置Entry", command=configure_entry) button.pack() root.mainloop()
entry.config() : 改變文本框部件的屬性
4、Text多行文本域
基本屬性
參數(shù) | 注釋 |
---|---|
bg | 背景色 |
fg | 底色 |
font | 字體(樣式,大?。?/td> |
width | 文本框?qū)挾?/td> |
bd | 邊框?qū)挾?/td> |
height | 文本框高度 |
wrap | 文本換行方式,可選WORD( 按單詞換行)或CHAR( 按字符換行) |
state | 狀態(tài),可選值為NORMAL (可編輯)或DISABLED (不可編輯) |
4.1 wrap,state
from tkinter import * root = Tk() root.geometry('500x500+400+200') root.title('胖兔always') text1 = Text(root, fg='white', bg='#393b40', font=('宋體', '15'), width=15, height=4, bd=3, wrap=CHAR) text1.grid(row=1, column=1) text2 = Text(root, fg='white', bg='#393b40', font=('宋體', '15'), width=15, height=4, bd=3, state=DISABLED) text2.grid(row=2, column=2) root.mainloop()
warp=CHAR : 按字符換行。無論單詞的位置如何,文本都會在字符之間自動換行,以確保整個字符不會被截斷。而WORD為單詞換行。當文本長度超過文本域的寬時,文本在單詞間自動換行
state: 狀態(tài),可選值為NORMAL
(可編輯)或DISABLED
(不可編輯)。
5、Frame 框架
基本屬性
參數(shù) | 注釋 |
---|---|
bg | 背景顏色 |
width | 寬度 |
height | 高度 |
relief | 三維效果 (參數(shù)值:FLAT、SUNKEN、RAISED、GROOVE、RIDGE。默認為 FLAT) |
bd | 邊框?qū)挾?/td> |
padx | 水平方向內(nèi)邊距 |
pady | 垂直方向內(nèi)邊距 |
5.1 基本使用
from tkinter import * root = Tk() root.geometry('500x500+400+200') root.title('胖兔always') frame = Frame(root, height=3, width=10, bd=5, relief='sunken') frame.pack() button = Button(frame, text='測試', bd=2) button.pack() text1 = Text(frame, fg='white', bg='#393b40', height=4, width=14, bd=3) text1.pack() root.mainloop()
Frame():
用于將其他小部件組織在一起并進行布局管理。 方便一些組件的統(tǒng)一管理調(diào)整。
5.2 方法展示:destroy()
from tkinter import * root = Tk() root.geometry('500x500+400+200') root.title('胖兔always') frame = Frame(root) frame.pack() label = Label(frame, text="標簽內(nèi)容.......") label.pack() def destroy_frame(): frame.destroy() destroy_button = Button(root, text="銷毀frame框架", command=destroy_frame) destroy_button.pack() root.mainloop()
destroy()
方法用于銷毀定義好的Frame
框架及其所有子部件??梢栽赥kinter應(yīng)用程序中動態(tài)地添加或移除框架及其子部件。
6、meun 菜單
基本屬性
參數(shù) | 注釋 |
---|---|
bg | 前景顏色 |
fg | 背景顏色 |
font | 字體(樣式,大?。?/td> |
width | 寬度 |
height | 高度 |
bd | 邊框?qū)挾?/td> |
tearoff | 是否顯示菜單的撕開標志,可選值為 0(不顯示)和 1(顯示) |
activebackground | 菜單項被激活時的背景顏色 |
activeforeground | 菜單項被激活時的前景顏色。 |
6.1 tearoff,activebackground,activeforeground
from tkinter import * root = Tk() root.geometry('500x500+400+200') root.title('胖兔always') menubar = Menu(root) filemenu = Menu(menubar, tearoff=0, bg='#2a2d30', fg='#bbbbbb') menubar.add_cascade(label='文件', menu=filemenu) # 添加一個級聯(lián)菜單,label為顯示的文本,menu為級聯(lián)的子菜單 filemenu.add_command(label='打開', activebackground='red', activeforeground='#323233') filemenu.add_separator() # 添加一個分隔線。 filemenu.add_command(label='保存') # 添加一個普通菜單項 root.config(menu=menubar) root.mainloop()
Menu()
是用于創(chuàng)建菜單的小部件。
tearoff=0 :不顯示菜單撕開標志
activebackground=‘red’ :當鼠標放上面時,背景顏色變紅色
activeforeground='#323233' :當鼠標放上面時,字體顏色變黑色
.add_cascade()lebal, menu, **options) :添加一個級聯(lián)菜單,label
為顯示的文本,menu
為級聯(lián)的子菜單。
.add_command(label, command, **options) :添加一個普通菜單項,label
為顯示的文本,command
為點擊后執(zhí)行的函數(shù)。
.add_separator() :添加一個分隔線。
6.2 方法展示:delete(index)
from tkinter import * root = Tk() root.geometry('500x500+400+200') root.title('胖兔always') menubar = Menu(root) def hello(): print("Hello World!") menu = Menu(root) root.config(menu=menu) file_menu = Menu(menu) menu.add_cascade(label="文件", menu=file_menu) file_menu.add_command(label="保存", command=hello) file_menu.add_command(label="打開", command=hello) # 刪除第二個菜單項(索引從0開始) file_menu.delete(1) root.mainloop()
delete(index):方法刪除菜單中指定索引位置的菜單項
delete(1) :方法刪除了第二個菜單項(索引為1)。刪除了"保存"菜單項。
command=hello :點擊觸發(fā)hello()函數(shù)操作
6.3 方法展示:insert_separator(index)
from tkinter import * root = Tk() root.geometry('500x500+400+200') root.title('胖兔always') menu = Menu(root) root.config(menu=menu) file_menu = Menu(menu) menu.add_cascade(label="文件", menu=file_menu) file_menu.add_command(label="打開") file_menu.add_command(label="保存") # 在第二個菜單項后插入一個分隔線 file_menu.insert_separator(2) file_menu.add_command(label="關(guān)閉") root.mainloop()
insert_separator(index)
方法用于在菜單中的指定索引位置插入一個分隔線
7、Canvas 繪畫
基本屬性
參數(shù) | 注釋 |
---|---|
bg | Canvas的背景顏色 |
width | Canvas的寬度 |
height | Canvas的高度 |
7.1 方法展示:create_line(),create_restangle(),create_oval(),create_text(),create_image()
方法 | 注釋 |
---|---|
create_line(x1, y1, x2, y2, options) | 在Canvas上創(chuàng)建一條直線。 |
create_rectangle(x1, y1, x2, y2, options) | 在Canvas上創(chuàng)建一個矩形。 |
create_oval(x1, y1, x2, y2, options) | 在Canvas上創(chuàng)建一個橢圓。 |
create_text(x, y, text, options) | 在Canvas上創(chuàng)建文本。 |
create_image(x, y, image, options) | 在Canvas上創(chuàng)建圖像。 |
from tkinter import * root = Tk() root.geometry('500x500+400+200') root.title('胖兔always') canvas = Canvas(root, bg="#f2e0dc", width=400, height=400) canvas.pack() # 創(chuàng)建一條直線 line = canvas.create_line(160, 50, 50, 40, fill="#23407b") # 創(chuàng)建一個矩形 rectangle = canvas.create_rectangle(80, 100, 250, 200, fill="#aa4926") # 創(chuàng)建一個橢圓 oval = canvas.create_oval(50, 250, 150, 350, fill="#578933") # 創(chuàng)建文本 text = canvas.create_text(200, 280, text="Hello World!", fill="#813f09") # 創(chuàng)建圖像 image = PhotoImage(file="./Aa_圖片素材庫/PNG50534.png") image = image.subsample(4) # 縮小為原來的1/4 canvas.create_image(340, 300, image=image) root.mainloop()
7.2 方法展示:delete(),move(),itemconfig()
方法 | 注釋 |
---|---|
delete(item) | 刪除Canvas上的指定項 |
move(item,dx,dy) | 移動Canvas上的指定項 |
itemconfig(item,**options) | 配置Canvas上的指定項 |
from tkinter import * root = Tk() root.geometry('500x500+400+200') root.title('胖兔always') canvas = Canvas(root, bg="#f2e0dc", width=400, height=400) canvas.pack() # 創(chuàng)建一個矩形 rectangle = canvas.create_rectangle(50, 30, 220, 150, fill="#aa4926") # 創(chuàng)建文本 text = canvas.create_text(140, 200, text="Hello World!", fill="#813f09") # 修改矩形的屬性 canvas.itemconfig(rectangle, fill="red", outline="black", width=2) # 移動圖形 canvas.move(rectangle, 50, 50) # 刪除文本 canvas.delete(text) root.mainloop()
.itemconfig(...) :將底色改為red,邊框顏色改為black,寬度為2
.move(...) :將圖形移動到 50,50 位置
.delete(...) :將text文字刪除
8、Messagebox 彈窗
基本屬性
方法 | 注釋 |
---|---|
messagebox.showinfo() | 顯示一個信息框 |
messagebox.showwarning() | 顯示一個警告框 |
messagebox.showerror() | 顯示一個錯誤框 |
messagebox.askquestion() | 顯示一個詢問框 |
8.1 基本應(yīng)用
from tkinter import * from tkinter import messagebox root = Tk() root.geometry('500x500+400+200') root.title('胖兔always') # 顯示信息框 messagebox.showinfo("信息框", "這是一個信息框") # 顯示警告框 messagebox.showwarning("警告框", "這是一個警告框") # 顯示錯誤框 messagebox.showerror("錯誤框", "這是一個錯誤框") # 顯示詢問框 response = messagebox.askquestion("詢問框", "是否要繼續(xù)?") if response == 'yes': print("用戶想要繼續(xù)。") else: print("用戶不想繼續(xù)。") root.mainloop()
messagebox
是Tkinter庫中用于顯示消息框的模塊,可以方便地創(chuàng)建各種類型的消息框,如提示框、警告框、錯誤框等
messagebox.showinfo(title, message)
:顯示一個信息框,包含指定的標題和消息。messagebox.showwarning(title, message)
:顯示一個警告框,包含指定的標題和消息。messagebox.showerror(title, message)
:顯示一個錯誤框,包含指定的標題和消息。messagebox.askquestion(title, message)
:顯示一個包含是/否按鈕的對話框,并返回用戶的選擇。
9、布局管理器
在Tkinter中,布局是指如何在窗口中安排和組織各個部件(如按鈕、標簽、文本框等)以及它們之間的空間關(guān)系。Tkinter提供了幾種布局管理器來幫助您實現(xiàn)不同的布局效果,其中最常用的是pack
、grid
和place
布局管理器。
9.1 Pack布局
Pack
布局管理器是Tkinter中最簡單的布局管理器,它按照添加部件的順序自動排列部件。您可以使用pack()
方法將部件添加到窗口,并可以通過指定side
參數(shù)來控制部件的排列方向(TOP
、BOTTOM
、LEFT
、RIGHT
)。
from tkinter import * root = Tk() root.geometry('500x500+400+200') root.title('胖兔always') btn1 = Button(root, text="按鈕 1") btn2 = Button(root, text="按鈕 2") btn1.pack(side="top") btn2.pack(side="left") root.mainloop()
pack布局如果不設(shè)置參數(shù),它將按照添加部件的順序自動排序
9.2 Grid布局
Grid
布局管理器允許您將部件放置在一個二維表格中,通過指定row
和column
參數(shù)來控制部件的位置。您還可以使用sticky
參數(shù)來指定部件在單元格中的對齊方式。
from tkinter import * root = Tk() root.geometry('500x500+400+200') root.title('胖兔always') btn1 = Button(root, text="Button 1") btn2 = Button(root, text="Button 2") btn3 = Button(root, text="Button 3") btn1.grid(row=0, column=0, sticky="nsew") btn2.grid(row=1, column=0, sticky="nsew") btn3.grid(row=2, column=1, sticky="nsew") # 注: 對其方式有N:上對齊(北), S:下對齊(南), E:右對齊(東), W:左對齊(西), NW:左上對齊(西北), NE:右上對齊(東北), SW:左下對齊(西南), SE:右下對齊(東南) # 如果您想要部件在單元格中居中對齊,可以使用sticky="NSEW" root.mainloop()
sticky可以不設(shè)置參數(shù)
# 注: 對其方式有N:上對齊(北), S:下對齊(南), E:右對齊(東), W:左對齊(西), NW:左上對齊(西北), NE:右上對齊(東北), SW:左下對齊(西南), SE:右下對齊(東南) # 如果您想要部件在單元格中居中對齊,可以使用sticky="NSEW"
9.3 Place布局
Place
布局管理器允許您精確地指定部件的位置和大小。通過指定x
、y
坐標和width
、height
參數(shù),您可以將部件放置在窗口的指定位置。
from tkinter import * root = Tk() root.geometry('500x500+400+200') root.title('胖兔always') btn1 = Button(root, text="Button 1") btn2 = Button(root, text="Button 2") btn1.place(x=10, y=10) btn2.place(x=50, y=50) root.mainloop()
布局軸向
總結(jié)
到此這篇關(guān)于python中Tkinter詳細基礎(chǔ)教學的文章就介紹到這了,更多相關(guān)python Tkinter基礎(chǔ)教學內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python3如何使用range函數(shù)替代xrange函數(shù)
這篇文章主要介紹了Python3如何使用range函數(shù)替代xrange函數(shù),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-10-10pandas進行數(shù)據(jù)的交集與并集方式的數(shù)據(jù)合并方法
今天小編就為大家分享一篇pandas進行數(shù)據(jù)的交集與并集方式的數(shù)據(jù)合并方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-06-06對django views中 request, response的常用操作詳解
今天小編就為大家分享一篇對django views中 request, response的常用操作詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07Python 3.x 判斷 dict 是否包含某鍵值的實例講解
今天小編就為大家分享一篇Python 3.x 判斷 dict 是否包含某鍵值的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07