欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python中Tkinter詳細(xì)基礎(chǔ)教學(xué)實(shí)例代碼

 更新時(shí)間:2024年12月21日 10:45:56   作者:胖兔always  
這篇文章主要給大家介紹了關(guān)于python中Tkinter詳細(xì)基礎(chǔ)教學(xué)的相關(guān)資料,文中介紹了如Label、Button、Entry、Text、Frame、Menu、Canvas、Messagebox等的基本屬性和用法,并介紹了布局管理器pack、grid和place的使用方法,需要的朋友可以參考下

前言

本文致力與幫助想要需要Tkinter的小伙伴,內(nèi)容詳細(xì)簡(jiǎn)介明了不臃腫,會(huì)詳細(xì)的介紹Tkinter的常用操作以及核心組件和它們的重要參數(shù)。想要學(xué)習(xí)的小伙伴一定要認(rèn)真觀(guān)看喔~

模塊導(dǎo)入

tk為python只帶的標(biāo)準(zhǔn)庫(kù),不需要下載,直接導(dǎo)入。

from tkinter import *

tkinter核心組件

label標(biāo)簽用來(lái)顯示文字或圖片
Button按鈕類(lèi)似標(biāo)簽,但提供額外的功能,例如鼠標(biāo)掠過(guò)、按下、釋放以及鍵盤(pán)操作、事件
 
Entry單行文字域用來(lái)收集鍵盤(pán)輸入
Text多行文字區(qū)域可用來(lái)收集(或顯示)用戶(hù)輸入的文字
Frame框架包含其他組件的純?nèi)萜?/td>
Checkbutton選擇按鈕一組方框,可以選擇其中的任意個(gè)
Listbox列表框一個(gè)選項(xiàng)列表,用戶(hù)可以從中選擇
Menu菜單點(diǎn)下菜單按鈕后彈出的一個(gè)選項(xiàng)列表,用戶(hù)可以從中選擇
Menubutton菜單按鈕用來(lái)包含菜單的組件(有下拉式、層疊式等等)
Message消息框類(lèi)似于標(biāo)簽,但可以顯示多行文本
Radiobutton單選按鈕組按鈕,其中只有一個(gè)可被“按下” (類(lèi)似 HTML 中的 radio)
Scale進(jìn)度條線(xiàn)性“滑塊”組件,可設(shè)定起始值和結(jié)束值,會(huì)顯示當(dāng)前位置的精確值
Scrollbar滾動(dòng)條對(duì)其支持的組件(文本域、畫(huà)布、列表框、文本框)提供滾動(dòng)功能
Toplevel頂級(jí)類(lèi)似框架,但提供一個(gè)獨(dú)立的窗口容器
Canvas繪畫(huà)提供繪圖功能(直線(xiàn)、橢圓、多邊形、矩形) 可以包含圖形或位圖

基礎(chǔ)架構(gòu)

第一步 導(dǎo)入tk模塊

from tkinter import *

第二步 獲取TK對(duì)象

root = Tk()

第三步 指定窗口大小位置

# 指定了窗口的寬度、高度和位置。
root.geometry('600x450+400+200') 
# 寬度為600像素
# 高度為450像素
# 位置在屏幕上的坐標(biāo)為(x=400, y=200)

第四步 主窗口標(biāo)題

# 主窗口的框體的標(biāo)題
root.title('title')

第五步 顯示主窗口

root.mainloop()  # 顯示主窗口

總和

from tkinter import *

root = Tk()
root.geometry('400x300+400+200')
root.title('title')

"""
此處寫(xiě)tk框架組件
"""
root.mainloop()

1Label 標(biāo)簽

基本屬性

參數(shù)
注釋
text標(biāo)簽名稱(chēng)
font字體(樣式,大?。?/td>
bg (background)背景顏色(標(biāo)簽顏色)
fg (foreground)前景顏色(字體顏色)
width標(biāo)簽寬度
height標(biāo)簽高度
anchor錨選項(xiàng),控制標(biāo)簽文本的位置(參數(shù)值:S,W,E,N,SE,SW,NW,NE,CENTER,默認(rèn)為CENTER)
bitmap位圖
relief三維效果(參數(shù)值:FLAT、SUNKEN、RAISED、GROOVE、RIDGE。默認(rèn)為 FLAT)
image與PhotoImage 一起使用,圖片只能為gif圖片
compound 圖片和文字一同顯示
padx設(shè)置文本與標(biāo)簽邊框x軸方向上距離
pady設(shè)置文本與標(biāo)簽邊框y軸方向上的距離
cursor鼠標(biāo)移動(dòng)到框架時(shí),光標(biāo)的形狀(參數(shù)值:arrow, circle, cross, plus 等)
justify顯示多行文本的時(shí)候,設(shè)置不同行之間的對(duì)齊方式(參數(shù)值:LEFT, RIGHT, CENTER)
state 設(shè)置標(biāo)簽狀態(tài),參數(shù)值:NORMAL、ACTIVE、 DISABLED。默認(rèn) NORMAL
wraplength指定每行文本的寬度,單位是屏幕單元
underline下劃線(xiàn)。默認(rèn)按鈕上的文本都不帶下劃線(xiàn)。取值就是帶下劃線(xiàn)的字符串索引,為 0 時(shí),第一個(gè)字符帶下劃線(xiàn),為 1 時(shí),第二個(gè)字符帶下劃線(xiàn),以此類(lèi)推

1.1  text,bg,font,fg

from tkinter import *

root = Tk()
root.geometry('400x300+400+200')
root.title('title')
# ------
label1 = Label(root, text='測(cè)試1', bg='purple', font=('華文行楷', 20), fg='blue')
label1.grid(row=1, column=1)
# ------
root.mainloop()

其中 grid(row=1,column=1) 代表著當(dāng)前標(biāo)簽位置于 第一行第一列

text為標(biāo)簽名“測(cè)試1”

bg為底色“purple”紫色

font為字體“華文行楷”以及字體大小“20”

fg為字體顏色“bule”藍(lán)色

1.2  width,height,anchor,padx

from tkinter import *

root = Tk()
root.geometry('400x300+400+200')
root.title('title')
# ------
label2 = Label(root, text='測(cè)試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) 代表著當(dāng)前標(biāo)簽位置于 第二行 第一列。而因?yàn)榍懊鏇](méi)有內(nèi)容行,因此標(biāo)簽顯示在第一行。

width為寬度“8”

height為高度"3"

anchor=E為文本在標(biāo)簽中的位置

padx=20為文本與標(biāo)簽邊框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_圖片素材庫(kù)/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 一起使用,為標(biāo)簽顯示當(dāng)前圖片,且圖片只能為gif圖片

compound為表示圖片和文字一同顯示,left表示圖片顯示在標(biāo)簽左邊

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位圖測(cè)試')
label5.grid(row=2, column=3)
# ------
root.mainloop()

bd指定標(biāo)簽部件的邊框?qū)挾?,這里設(shè)置為3像素

bitmap用于指定在標(biāo)簽部件中顯示的位圖。'error'表示顯示一個(gè)帶有問(wèn)題圖標(biāo)的位圖。它們有如下'error'、'info'、'question'等,分別代表錯(cuò)誤、信息和問(wèn)題圖標(biāo)。也可以自定義的位圖文件。

2、Button 按鈕

基本屬性

參數(shù)注釋
text按鈕文本內(nèi)容
font字體(樣式,大小)
bg背景顏色(按鈕顏色)
fg 前景顏色(字體顏色)
width按鈕寬度
height按鈕高度
command按鈕關(guān)聯(lián)的函數(shù),當(dāng)按鈕被點(diǎn)擊時(shí),執(zhí)行該函數(shù)
padx 設(shè)置按鈕文本與按鈕邊框x軸方向的距離
pady設(shè)置按鈕文本與按鈕邊框y軸方向的距離
bd按鈕邊框?qū)挾?/td>
anchor控制按鈕文本的位置(參數(shù)值:S,W,E,N,SE,SW,NW,NE,CENTER,默認(rèn)為CENTER)
image與PhotoImage 一起使用,圖片只能為gif格式
relief三維效果 (參數(shù)值:FLAT、SUNKEN、RAISED、GROOVE、RIDGE。默認(rèn)為 FLAT)
bitmap位圖
compound圖片和文字一同顯示
cursor 鼠標(biāo)移動(dòng)到框架時(shí),光標(biāo)的形狀(參數(shù)值:arrow, circle, cross, plus 等)
justify顯示多行文本的時(shí)候,設(shè)置不同行之間的對(duì)齊方式(參數(shù)值:LEFT, RIGHT, CENTER)
state設(shè)置按鈕狀態(tài),參數(shù)值:NORMAL、ACTIVE、 DISABLED。默認(rèn) NORMAL
wraplength指定每行文本的寬度,單位是屏幕單元
underline下劃線(xiàn)。默認(rèn)按鈕上的文本都不帶下劃線(xiàn)。取值就是帶下劃線(xiàn)的字符串索引,為 0 時(shí),第一個(gè)字符帶下劃線(xiàn),為 1 時(shí),第二個(gè)字符帶下劃線(xiàn),以此類(lèi)推

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='測(cè)試1', bg='purple', font=('華文行楷', 20), fg='blue', width=8, height=2)
button1.grid(row=2, column=1)

root.mainloop()

這些屬性功能基本與標(biāo)簽屬性類(lèi)型,分別為按鈕名,文字樣式,底色,按鈕文字顏色,寬和高。

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('提示', '已點(diǎn)擊 測(cè)試1 按鈕')


button1 = Button(root, text='測(cè)試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" 意思是當(dāng)點(diǎn)擊此按鈕時(shí)觸發(fā)函數(shù)test,其中test函數(shù)意義為顯示出一個(gè)彈窗。是tk中重要的按鈕屬性

padx為按鈕內(nèi)文字與按鈕邊框x軸的距離

pady為按鈕內(nèi)文字與按鈕邊框y軸的距離

bd為按鈕邊框?qū)挾?/p>

"anchor=E"表示按鈕上的文本將沿著按鈕的右側(cè)對(duì)齊,其中默認(rèn)為CENTER居中

2.3  image,relief,bitmap,compound,cursor

from tkinter import *

root = Tk()
root.geometry('400x300+400+200')

button1 = Button(root, text='測(cè)試', cursor='cross', relief=SUNKEN, bitmap='question',)
button1.grid(row=1, column=2)

picture = PhotoImage(file=r'Aa_圖片素材庫(kù)/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)楫?dāng)前gif動(dòng)態(tài)圖

relief將當(dāng)前按鈕以三維效果顯示,此代碼意思是將問(wèn)號(hào)按鈕沉沒(méi)顯示,默認(rèn)為平。

bitmap設(shè)置按鈕的位圖。位圖是一種小的圖像,通常用于表示簡(jiǎn)單的圖標(biāo)或符號(hào)。將位圖文件的路徑傳遞給bitmap參數(shù)?;蚴褂胻k自帶的位圖。

cursor則表示鼠標(biāo)放當(dāng)前按鈕上時(shí)顯示樣式

2.4  justify,state,wraplength,underline

from tkinter import *

root = Tk()
root.geometry('400x300+400+200')

button1 = Button(root, text='測(cè)試多行文字展示多行文字展示', 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)也是默認(rèn)狀態(tài):

warplength=60:表示么行文本寬度為100(單位屏幕單元)

underlinr=0:表示按鈕第一個(gè)文字帶下劃線(xiàn)

3、Enter單行文字域

基本屬性

參數(shù)注釋
bg背景色
fg底色
font字體(樣式,大?。?/td>
width文本框?qū)挾?/td>
bd文本框框?qū)?/td>
show輸入顯示方式
textvariable將一個(gè)StringVar對(duì)象與Entry小部件關(guān)聯(lián)起來(lái),以便在用戶(hù)輸入文本時(shí)實(shí)時(shí)更新

3.1  show,textvariable

from tkinter import *

root = Tk()
root.geometry('500x500+400+200')

# 創(chuàng)建一個(gè)StringVar對(duì)象
text_var = StringVar()

# 將StringVar對(duì)象與Entry小部件關(guān)聯(lián)
entry = Entry(root, textvariable=text_var, width=30, show="*")
entry.pack()


def get_text():
    text = text_var.get()
    print("用戶(hù)輸入的文本是:", 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) : 從開(kāi)頭到末尾的所有文本內(nèi)容。這個(gè)方法調(diào)用會(huì)清空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字體(樣式,大小)
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 : 按字符換行。無(wú)論單詞的位置如何,文本都會(huì)在字符之間自動(dòng)換行,以確保整個(gè)字符不會(huì)被截?cái)唷6鳺ORD為單詞換行。當(dāng)文本長(zhǎng)度超過(guò)文本域的寬時(shí),文本在單詞間自動(dòng)換行

state: 狀態(tài),可選值為NORMAL(可編輯)或DISABLED(不可編輯)。

5、Frame 框架

基本屬性

參數(shù)注釋
bg背景顏色
width寬度
height高度
relief三維效果 (參數(shù)值:FLAT、SUNKEN、RAISED、GROOVE、RIDGE。默認(rèn)為 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='測(cè)試', bd=2)
button.pack()
text1 = Text(frame, fg='white', bg='#393b40', height=4, width=14, bd=3)
text1.pack()

root.mainloop()

Frame():用于將其他小部件組織在一起并進(jìn)行布局管理。 方便一些組件的統(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="標(biāo)簽內(nèi)容.......")
label.pack()


def destroy_frame():
    frame.destroy()


destroy_button = Button(root, text="銷(xiāo)毀frame框架", command=destroy_frame)
destroy_button.pack()

root.mainloop()

destroy()方法用于銷(xiāo)毀定義好的Frame框架及其所有子部件??梢栽赥kinter應(yīng)用程序中動(dòng)態(tài)地添加或移除框架及其子部件。 

6、meun 菜單

基本屬性

參數(shù)注釋
bg前景顏色
fg背景顏色
font字體(樣式,大小)
width寬度
height高度
bd邊框?qū)挾?/td>
tearoff是否顯示菜單的撕開(kāi)標(biāo)志,可選值為 0(不顯示)和 1(顯示)
activebackground菜單項(xiàng)被激活時(shí)的背景顏色
activeforeground菜單項(xiàng)被激活時(shí)的前景顏色。

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)  # 添加一個(gè)級(jí)聯(lián)菜單,label為顯示的文本,menu為級(jí)聯(lián)的子菜單
filemenu.add_command(label='打開(kāi)', activebackground='red', activeforeground='#323233')
filemenu.add_separator()  # 添加一個(gè)分隔線(xiàn)。
filemenu.add_command(label='保存')  # 添加一個(gè)普通菜單項(xiàng)

root.config(menu=menubar)
root.mainloop()

 Menu()是用于創(chuàng)建菜單的小部件。

tearoff=0 :不顯示菜單撕開(kāi)標(biāo)志

activebackground=‘red’ :當(dāng)鼠標(biāo)放上面時(shí),背景顏色變紅色

activeforeground='#323233'  :當(dāng)鼠標(biāo)放上面時(shí),字體顏色變黑色

.add_cascade()lebal, menu, **options) :添加一個(gè)級(jí)聯(lián)菜單,label為顯示的文本,menu為級(jí)聯(lián)的子菜單。

.add_command(label, command, **options) :添加一個(gè)普通菜單項(xiàng),label為顯示的文本,command為點(diǎn)擊后執(zhí)行的函數(shù)。

.add_separator() :添加一個(gè)分隔線(xiàn)。

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="打開(kāi)", command=hello)

# 刪除第二個(gè)菜單項(xiàng)(索引從0開(kāi)始)
file_menu.delete(1)

root.mainloop()

delete(index):方法刪除菜單中指定索引位置的菜單項(xiàng) 

delete(1)  :方法刪除了第二個(gè)菜單項(xiàng)(索引為1)。刪除了"保存"菜單項(xiàng)。

command=hello :點(diǎn)擊觸發(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="打開(kāi)")
file_menu.add_command(label="保存")

# 在第二個(gè)菜單項(xiàng)后插入一個(gè)分隔線(xiàn)
file_menu.insert_separator(2)

file_menu.add_command(label="關(guān)閉")

root.mainloop()

 insert_separator(index)方法用于在菜單中的指定索引位置插入一個(gè)分隔線(xiàn)

7、Canvas 繪畫(huà)

基本屬性

參數(shù)注釋
bgCanvas的背景顏色
widthCanvas的寬度
heightCanvas的高度

7.1  方法展示:create_line(),create_restangle(),create_oval(),create_text(),create_image()

方法注釋
create_line(x1, y1, x2, y2, options)在Canvas上創(chuàng)建一條直線(xiàn)。
create_rectangle(x1, y1, x2, y2, options)在Canvas上創(chuàng)建一個(gè)矩形。
create_oval(x1, y1, x2, y2, options)在Canvas上創(chuàng)建一個(gè)橢圓。
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)建一條直線(xiàn)
line = canvas.create_line(160, 50, 50, 40, fill="#23407b")

# 創(chuàng)建一個(gè)矩形
rectangle = canvas.create_rectangle(80, 100, 250, 200, fill="#aa4926")

# 創(chuàng)建一個(gè)橢圓
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_圖片素材庫(kù)/PNG50534.png")
image = image.subsample(4)  # 縮小為原來(lái)的1/4

canvas.create_image(340, 300, image=image)

root.mainloop()

7.2  方法展示:delete(),move(),itemconfig()

方法注釋
delete(item)刪除Canvas上的指定項(xiàng)
move(item,dx,dy)移動(dòng)Canvas上的指定項(xiàng)
itemconfig(item,**options)配置Canvas上的指定項(xià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)建一個(gè)矩形
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)
# 移動(dòng)圖形
canvas.move(rectangle, 50, 50)
# 刪除文本
canvas.delete(text)

root.mainloop()

.itemconfig(...) :將底色改為red,邊框顏色改為black,寬度為2

.move(...) :將圖形移動(dòng)到 50,50 位置

.delete(...) :將text文字刪除

8、Messagebox 彈窗

基本屬性

方法注釋
messagebox.showinfo()顯示一個(gè)信息框
messagebox.showwarning()顯示一個(gè)警告框
messagebox.showerror()顯示一個(gè)錯(cuò)誤框
messagebox.askquestion()顯示一個(gè)詢(xún)問(wèn)框

8.1  基本應(yīng)用 

from tkinter import *
from tkinter import messagebox

root = Tk()
root.geometry('500x500+400+200')
root.title('胖兔always')

# 顯示信息框
messagebox.showinfo("信息框", "這是一個(gè)信息框")

# 顯示警告框
messagebox.showwarning("警告框", "這是一個(gè)警告框")

# 顯示錯(cuò)誤框
messagebox.showerror("錯(cuò)誤框", "這是一個(gè)錯(cuò)誤框")

# 顯示詢(xún)問(wèn)框
response = messagebox.askquestion("詢(xún)問(wèn)框", "是否要繼續(xù)?")
if response == 'yes':
    print("用戶(hù)想要繼續(xù)。")
else:
    print("用戶(hù)不想繼續(xù)。")

root.mainloop()

 messagebox是Tkinter庫(kù)中用于顯示消息框的模塊,可以方便地創(chuàng)建各種類(lèi)型的消息框,如提示框、警告框、錯(cuò)誤框等

  • messagebox.showinfo(title, message):顯示一個(gè)信息框,包含指定的標(biāo)題和消息。
  • messagebox.showwarning(title, message):顯示一個(gè)警告框,包含指定的標(biāo)題和消息。
  • messagebox.showerror(title, message):顯示一個(gè)錯(cuò)誤框,包含指定的標(biāo)題和消息。
  • messagebox.askquestion(title, message):顯示一個(gè)包含是/否按鈕的對(duì)話(huà)框,并返回用戶(hù)的選擇。

9、布局管理器

在Tkinter中,布局是指如何在窗口中安排和組織各個(gè)部件(如按鈕、標(biāo)簽、文本框等)以及它們之間的空間關(guān)系。Tkinter提供了幾種布局管理器來(lái)幫助您實(shí)現(xiàn)不同的布局效果,其中最常用的是pack、gridplace布局管理器。

9.1  Pack布局

Pack布局管理器是Tkinter中最簡(jiǎn)單的布局管理器,它按照添加部件的順序自動(dòng)排列部件。您可以使用pack()方法將部件添加到窗口,并可以通過(guò)指定side參數(shù)來(lái)控制部件的排列方向(TOP、BOTTOMLEFT、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ù),它將按照添加部件的順序自動(dòng)排序

9.2 Grid布局

Grid布局管理器允許您將部件放置在一個(gè)二維表格中,通過(guò)指定rowcolumn參數(shù)來(lái)控制部件的位置。您還可以使用sticky參數(shù)來(lái)指定部件在單元格中的對(duì)齊方式。

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")
# 注: 對(duì)其方式有N:上對(duì)齊(北), S:下對(duì)齊(南), E:右對(duì)齊(東), W:左對(duì)齊(西), NW:左上對(duì)齊(西北), NE:右上對(duì)齊(東北), SW:左下對(duì)齊(西南), SE:右下對(duì)齊(東南)
# 如果您想要部件在單元格中居中對(duì)齊,可以使用sticky="NSEW"
root.mainloop()

 sticky可以不設(shè)置參數(shù)

# 注: 對(duì)其方式有N:上對(duì)齊(北), S:下對(duì)齊(南), E:右對(duì)齊(東), W:左對(duì)齊(西), NW:左上對(duì)齊(西北), NE:右上對(duì)齊(東北), SW:左下對(duì)齊(西南), SE:右下對(duì)齊(東南)
# 如果您想要部件在單元格中居中對(duì)齊,可以使用sticky="NSEW"

9.3  Place布局

Place布局管理器允許您精確地指定部件的位置和大小。通過(guò)指定x、y坐標(biāo)和widthheight參數(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詳細(xì)基礎(chǔ)教學(xué)的文章就介紹到這了,更多相關(guān)python Tkinter基礎(chǔ)教學(xué)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • pandas如何讀取mysql數(shù)據(jù)

    pandas如何讀取mysql數(shù)據(jù)

    這篇文章主要介紹了pandas如何讀取mysql數(shù)據(jù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-12-12
  • Python實(shí)現(xiàn)的生成格雷碼功能示例

    Python實(shí)現(xiàn)的生成格雷碼功能示例

    這篇文章主要介紹了Python實(shí)現(xiàn)的生成格雷碼功能,結(jié)合實(shí)例形式分析了格雷碼的原理與Python相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2018-01-01
  • Django 配置多站點(diǎn)多域名的實(shí)現(xiàn)步驟

    Django 配置多站點(diǎn)多域名的實(shí)現(xiàn)步驟

    這篇文章主要介紹了Django 配置多站點(diǎn)多域名的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Python3如何使用range函數(shù)替代xrange函數(shù)

    Python3如何使用range函數(shù)替代xrange函數(shù)

    這篇文章主要介紹了Python3如何使用range函數(shù)替代xrange函數(shù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • python中使用%與.format格式化文本方法解析

    python中使用%與.format格式化文本方法解析

    這篇文章主要介紹了python中使用%與.format格式化文本方法解析,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2017-12-12
  • pandas進(jìn)行數(shù)據(jù)的交集與并集方式的數(shù)據(jù)合并方法

    pandas進(jìn)行數(shù)據(jù)的交集與并集方式的數(shù)據(jù)合并方法

    今天小編就為大家分享一篇pandas進(jìn)行數(shù)據(jù)的交集與并集方式的數(shù)據(jù)合并方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-06-06
  • 一篇文章徹底弄懂Python字符編碼

    一篇文章徹底弄懂Python字符編碼

    這篇文章主要介紹了一篇文章徹底弄懂Python字符編碼,各種常用的字符編碼的特點(diǎn),并介紹了在python2.x中如何與編碼問(wèn)題作戰(zhàn)?,下文詳細(xì)介紹需要的小伙伴可以參考一下
    2022-03-03
  • 對(duì)django views中 request, response的常用操作詳解

    對(duì)django views中 request, response的常用操作詳解

    今天小編就為大家分享一篇對(duì)django views中 request, response的常用操作詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-07-07
  • 如何用python整理附件

    如何用python整理附件

    本篇文章給大家整理了關(guān)于如何用python整理附件的相關(guān)知識(shí)點(diǎn),學(xué)習(xí)python的朋友可以跟著測(cè)試下。
    2018-05-05
  • Python 3.x 判斷 dict 是否包含某鍵值的實(shí)例講解

    Python 3.x 判斷 dict 是否包含某鍵值的實(shí)例講解

    今天小編就為大家分享一篇Python 3.x 判斷 dict 是否包含某鍵值的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-07-07

最新評(píng)論