網站首頁 編程語言 正文
1、Tkinter是什么
Tkinter 是使用 python 進行窗口視窗設計的模塊。Tkinter模塊(“Tk 接口”)是Python的標準Tk GUI工具包的接口。作為 python 特定的GUI界面,是一個圖像的窗口,tkinter是python自帶的,可以編輯的GUI界面
2、Tkinter創建窗口
①導入 tkinter的庫 ,創建并顯示窗口
import tkinter as tk # 在代碼里面導入庫,起一個別名,以后代碼里面就用這個別名
root = tk.Tk() # 這個庫里面有Tk()這個方法,這個方法的作用就是創建一個窗口
root.mainloop() # 加上這一句,就可以看見窗口了,循環顯示窗口
②修改窗口屬性
root.title('演示窗口') #設置窗口標題
root.geometry("300x100+630+80") # 設置窗口長x寬+x*y
③創建按鈕
import tkinter as tk #tk代替tkinter
from tkinter import messagebox
root = tk.Tk() # 創建窗口
root.title('演示窗口')
root.geometry("300x100+630+80") # 長x寬+x*y
btn1 = tk.Button(root) # 創建按鈕,并且將按鈕放到窗口里面
btn1["text"] = "點擊" # 給按鈕一個名稱
btn1.pack() # 按鈕布局
def test(e):
'''創建彈窗'''
messagebox.showinfo("窗口名稱", "點擊成功")
btn1.bind("<Button-1>", test) # 將按鈕和方法進行綁定,也就是創建了一個事件
root.mainloop() # 讓窗口一直顯示,循環
④窗口內的組件布局
3、Tkinter布局用法
①基本界面、label(標簽)和button(按鈕)用法
import tkinter as tk
root = tk.Tk()
root.title("這是Demo")
root.geometry("300x200")
var = tk.StringVar() #tk專有字符串
#root---界面(需要放在那個界面),textvariable----顯示文本
#bg----背景 font----字體格式,大小 width----寬度 height----高度
lab = tk.Label(root,textvariable=var,bg = 'green',font=('Arial',12),width=15,height=2)
lab.pack() #把標簽置入root界面布局
on_hit = True #點擊標志位
def hit_me():
global on_hit
if on_hit==True:
on_hit = False #點擊交替
var.set("you hit me")
else:
on_hit = True
var.set("")
#root---界面(需要放在那個界面),text----顯示文本
#width----寬度 height----高度 command----命令(執行哪個函數)
but = tk.Button(root,text = "hitme",width=15,height=2,command=hit_me)
but.pack()
root.mainloop()
②entry(輸入)和text(文本)用法
import tkinter as tk
root = tk.Tk()
root.title("這是Demo")
root.geometry("300x200")
#root---界面(需要放在那個界面),show----輸入的字符顯示為*,可以設置成show=none
e = tk.Entry(root,show='*')
e.pack()
def insert_point():
var = e.get()
t1.insert('insert',var)
def insert_end():
var = e.get()
t1.insert('end',var)
#root---界面(需要放在那個界面),text----顯示文本
#width----寬度 height----高度 command----命令(執行哪個函數)
b1 = tk.Button(root,text = "insert point",width=15,height=2,command=insert_point)
b1.pack()
b2= tk.Button(root,text = "insert end",width=15,height=2,command=insert_end)
b2.pack()
#root---界面(需要放在那個界面),height ----文本框寬度
t1 = tk.Text(root,height=2)
t1.pack(fill="x")
root.mainloop()
③var(變量)和list(列表)用法
import tkinter as tk
root = tk.Tk()
root.title("這是Demo")
root.geometry("300x200")
#root---界面(需要放在那個界面),textvariable----顯示文本變量
#bg----背景 font----字體格式,大小 width----寬度 height----高度
var1 = tk.StringVar()
l = tk.Label(root, bg='yellow', width=4, textvariable=var1)
l.pack()
def print_selection():
value = lb.get(lb.curselection()) #獲取列表選項
var1.set(value) #設置textvariable=var1的文本值
#root---界面(需要放在那個界面),text----顯示文本
#width----寬度 height----高度 command----命令(執行哪個函數)
b1 = tk.Button(root,text = "print selection",width=15,height=2,command=print_selection)
b1.pack()
var2 = tk.StringVar()
var2.set((11,22,33,44))
#root---界面(需要放在那個界面),listvariable----列表變量
lb = tk.Listbox(root, listvariable=var2) #列表變量var2
list_items = [1,2,3,4]
for item in list_items: #列表遍歷
lb.insert('end', item)
lb.insert(1, 'first') #1號位后加入first
lb.insert(2, 'second') #2號位后加入second
#lb.delete(2) #刪除2后的second
lb.pack()
root.mainloop()
④Radiobutton(選擇按鈕)用法
import tkinter as tk
root = tk.Tk()
root.title("這是Demo")
root.geometry("300x200")
#root---界面(需要放在那個界面),textvariable----顯示文本變量
#bg----背景 font----字體格式,大小 width----寬度 height----高度
var = tk.StringVar()
l = tk.Label(root, bg='yellow', width=20, text='empty')
l.pack()
def print_selection():
l.config(text='you have selected ' + var.get())
#Radiobutton-----選擇按鈕
#root---界面(需要放在那個界面),variable----變量
#value----值 command----命令
r1 = tk.Radiobutton(root, text='Option A',
variable=var, value='A',
command=print_selection)
r1.pack()
r2 = tk.Radiobutton(root, text='Option B',
variable=var, value='B',
command=print_selection)
r2.pack()
r3 = tk.Radiobutton(root, text='Option C',
variable=var, value='C',
command=print_selection)
r3.pack()
root.mainloop()
⑤Scale(尺度)用法
import tkinter as tk
root = tk.Tk()
root.title("這是Demo")
root.geometry("300x200")
#root---界面(需要放在那個界面),textvariable----顯示文本變量
#bg----背景 font----字體格式,大小 width----寬度 height----高度
l = tk.Label(root, bg='yellow', width=20, text='empty')
l.pack()
def print_selection(v):
l.config(text='you have selected ' + v)
#Scale----尺度函數
#root---界面(需要放在那個界面),label----標簽名字 from_ ----尺度從哪開始 to----尺度到哪結束
#orient----尺度方向(HORIZONTAL水平) length----顯示長度(單位為像素) tickinterval----每隔多少加個尺度顯示
#resolution ----精度(精確到小數點后多少) command----命令
s = tk.Scale(root, label='try me', from_=5, to=11, orient=tk.HORIZONTAL,
length=200, showvalue=0, tickinterval=2, resolution=0.01, command=print_selection)
s.pack()
root.mainloop()
⑥ Checkbutton(勾選項)用法
import tkinter as tk
root = tk.Tk()
root.title("這是Demo")
root.geometry("300x200")
#root---界面(需要放在那個界面),textvariable----顯示文本變量
#bg----背景 font----字體格式,大小 width----寬度 height----高度
l = tk.Label(root, bg='yellow', width=20, text='empty')
l.pack()
def print_selection():
if (var1.get() == 1) & (var2.get() == 0):
l.config(text='I love only Python ')
elif (var1.get() == 0) & (var2.get() == 1):
l.config(text='I love only C++')
elif (var1.get() == 0) & (var2.get() == 0):
l.config(text='I do not love either')
else:
l.config(text='I love both')
var1 = tk.IntVar()
var2 = tk.IntVar()
#Checkbutton----勾選函數
#root---界面(需要放在那個界面),text----文本 variable ----變量
#onvalue----勾選標志值 offvalue----無勾選標志值 command----命令
c1 = tk.Checkbutton(root, text='Python', variable=var1, onvalue=1, offvalue=0,
command=print_selection)
c2 = tk.Checkbutton(root, text='C++', variable=var2, onvalue=1, offvalue=0,
command=print_selection)
c1.pack()
c2.pack()
root.mainloop()
⑦canvas(畫布)用法
import tkinter as tk
root = tk.Tk()
root.title("這是Demo")
root.geometry("300x300")
#Canvas----畫布
#root---界面(需要放在那個界面)
#bg----背景 width----寬度,height----高度(單位像素)
canvas = tk.Canvas(root, bg='white', height=200, width=200)
#PhotoImage----加載圖片
#圖片路徑為file='C:\\Users\\admin\\Desktop\\Demo\\Demo\\1.gif' 注意是使用雙斜桿\\
image_file = tk.PhotoImage(file='C:\\Users\\admin\\Desktop\\Demo\\Demo\\1.gif')
#create_image----圖片位置
#create_image(瞄點橫坐標,瞄點縱坐標,瞄點位置,加入圖片)
image = canvas.create_image(0,0, anchor='nw', image=image_file)
#create_line----畫線
#create_oval----畫圓
#create_arc----畫扇形
#create_rectangle----畫矩形
#canvas.create_oval
x0, y0, x1, y1= 50, 50, 80, 80
line = canvas.create_line(x0, y0, x1, y1)
#create_oval----畫扇形
#create_oval(坐標,坐標,坐標,坐標,填充顏色)
oval = canvas.create_oval(x0, y0, x1, y1, fill='red')
#create_oval----畫圓
#create_oval(坐標,坐標,坐標,坐標,開始角度,結束角度)
arc = canvas.create_arc(x0+30, y0+30, x1+30, y1+30, start=90, extent=180)
rect = canvas.create_rectangle(100, 30, 100+20, 30+20)
canvas.pack()
#move----移動
#canvas.move(對象, 橫坐標, 縱坐標)
def moveit():
canvas.move(rect, 2, 4)
b = tk.Button(root, text='move', command=moveit).pack()
root.mainloop()
⑧menubar(菜單欄)
import tkinter as tk
root = tk.Tk()
root.title('my window')
root.geometry('300x300')
#root---界面(需要放在那個界面),textvariable----顯示文本變量
#bg----背景 font----字體格式,大小 width----寬度 height----高度
l = tk.Label(root, text='', bg='yellow')
l.pack()
counter = 0
def do_job():
global counter
l.config(text='do '+ str(counter))
counter+=1
#menubar----菜單
menubar = tk.Menu(root) #菜單加入root界面
filemenu = tk.Menu(menubar, tearoff=0) #filemenu加入菜單欄menubar下(tearoff=0 不可分割)
#filemenu菜單加入標簽和相應執行命令
menubar.add_cascade(label='File', menu=filemenu)
filemenu.add_command(label='New', command=do_job)
filemenu.add_command(label='Open', command=do_job)
filemenu.add_command(label='Save', command=do_job)
filemenu.add_separator() #添加分割線
filemenu.add_command(label='Exit', command=root.quit)
editmenu = tk.Menu(menubar, tearoff=0)#editmenu加入菜單menubar下(tearoff=0 不可分割)
#editmenu菜單加入標簽和相應執行命令
menubar.add_cascade(label='Edit', menu=editmenu)
editmenu.add_command(label='Cut', command=do_job)
editmenu.add_command(label='Copy', command=do_job)
editmenu.add_command(label='Paste', command=do_job)
#submenu加入菜單filemenu下(tearoff=0 不可分割)
submenu = tk.Menu(filemenu)
#filemenu菜單加入標簽和相應執行命令
filemenu.add_cascade(label='Import', menu=submenu, underline=0)
submenu.add_command(label="Submenu1", command=do_job)
#配置菜單欄
root.config(menu=menubar)
root.mainloop()
⑨Frame(架構)
import tkinter as tk
root = tk.Tk()
root.title('my window')
root.geometry('200x200')
#root---界面(需要放在那個界面),textvariable----顯示文本變量
#bg----背景 font----字體格式,大小 width----寬度 height----高度
tk.Label(root, text='on the window').pack()
#Frame----框架
#Frame的作用可以合理分布控件位置
#框架設置在root界面上
frm = tk.Frame(root)
frm.pack()
#框架設置在Frame框架上(分別在左右)
frm_l = tk.Frame(frm)
frm_r = tk.Frame(frm)
frm_l.pack(side='left')
frm_r.pack(side='right')
tk.Label(frm_l, text='on the frm_l1').pack()
tk.Label(frm_l, text='on the frm_l2').pack()
tk.Label(frm_r, text='on the frm_r1').pack()
root.mainloop()
⑩messagebox(彈窗)
import tkinter as tk
import tkinter.messagebox
root = tk.Tk()
root.title('my window')
root.geometry('200x200')
def hit_me():
#tk.messagebox.showinfo(title='Hi', message='hahahaha') # return 'ok'
#tk.messagebox.showwarning(title='Hi', message='nononono') #警告 return 'ok'
tk.messagebox.showerror(title='Hi', message='No!! never') #錯誤 return 'ok'
#print(tk.messagebox.askquestion(title='Hi', message='hahahaha')) #詢問 return 'yes' , 'no'
#print(tk.messagebox.askyesno(title='Hi', message='hahahaha')) #是否 return True, False
#print(tk.messagebox.askretrycancel(title='Hi', message='hahahaha')) # 重試 return True, False
#print(tk.messagebox.askokcancel(title='Hi', message='hahahaha')) # return True, False
# print(tk.messagebox.askyesnocancel(title="Hi", message="haha")) # return, True, False, None
tk.Button(root,text = 'hit_me',command=hit_me).pack()
root.mainloop()
⑾pack、grid以及place布局用法
import tkinter as tk
root = tk.Tk()
root.title('my window')
root.geometry('200x200')
#canvas = tk.Canvas(window, height=150, width=500)
#canvas.grid(row=1, column=1)
#image_file = tk.PhotoImage(file='welcome.gif')
#image = canvas.create_image(0, 0, anchor='nw', image=image_file)
#pack布局----可以通過side控制放在上下左右
#tk.Label(window, text='1').pack(side='top')
#tk.Label(window, text='1').pack(side='bottom')
#tk.Label(window, text='1').pack(side='left')
#tk.Label(window, text='1').pack(side='right')
#grid布局----通過方格的格式,將控件分布于其中
##for i in range(4):
#for j in range(3):
#tk.Label(window, text=1).grid(row=i, column=j, padx=10, pady=10)
#place布局----通過設置坐標方式,可以精確分布控件
tk.Label(root, text=1).place(x=20, y=10, anchor='nw')
root.mainloop()
總結
原文鏈接:https://blog.csdn.net/weixin_43626082/article/details/125189531
相關推薦
- 2022-07-11 CentOS 7安裝SQL Server 2019
- 2022-10-03 react進階教程之異常處理機制error?Boundaries_React
- 2023-02-06 Go語言基礎學習之指針詳解_Golang
- 2022-12-06 C++如何將字符串顛倒輸出_C 語言
- 2022-08-07 Python算法練習之二分查找算法的實現_python
- 2022-10-14 linux【centos 7】 yum 安裝 tesseract 4.1
- 2023-12-26 layui彈窗傳值
- 2023-05-03 深入了解一下C語言中的柔性數組_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支