自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

聊聊Python中的GUI布局Tkinter

開發(fā) 后端
現(xiàn)在極少有人會(huì)用上tkinter了,所以真正研究的人也就更少了,本來不想更新tkinter??吹胶芏嗳嗽趯W(xué)tkinter,其實(shí)用Python做布局,沒有人這么干。但還是更新幾節(jié)tkinter,在Python從入門到大師教程中來。

[[354919]]

 現(xiàn)在極少有人會(huì)用上tkinter了,所以真正研究的人也就更少了,本來不想更新tkinter。看到很多人在學(xué)tkinter,其實(shí)用Python做布局,沒有人這么干。但還是更新幾節(jié)tkinter,在Python從入門到大師教程中來。

tkinter

Tkinter包是Python附帶的標(biāo)準(zhǔn)軟件包,所以我們不需要安裝任何東西就可以使用它。

窗口主體框架

每一個(gè) tkinter 應(yīng)用的主體框架都可以包含下面這部分. 定義 window 窗口 和 window的一些屬性, 然后書寫窗口內(nèi)容, 最后執(zhí)行window.mainloop讓窗口活起來.

  1. import tkinter as tk 
  2. window = tk.Tk() 
  3. window.title('my window'
  4. window.geometry('200x100'
  5.  
  6. # 這里是窗口的內(nèi)容 
  7. window.mainloop()

窗口內(nèi)容

這次我們會(huì)建立一個(gè)用來描述的標(biāo)簽 tk.Label, 比如:

  1. import tkinter as tk 
  2. window = tk.Tk() 
  3. window.title('my window'
  4. window.geometry('200x100'
  5.  
  6.  
  7.  
  8. l = tk.Label(window,  
  9.     text='OMG! this is TK!',    # 標(biāo)簽的文字 
  10.     bg='green',     # 背景顏色 
  11.     font=('Arial', 12),     # 字體和字體大小 
  12.     width=15, height=2  # 標(biāo)簽長寬 
  13.     ) 
  14. l.pack()    # 固定窗口位置 
  15.  
  16. window.mainloop() 

 

控件

上面的Label就是一個(gè)控件,還有很多的,如按鈕,標(biāo)簽和文本框等,如下圖所示


控件自帶的共同屬性,如大小,字體和顏色等??筛鶕?jù)控件展現(xiàn)形式選擇相應(yīng)的屬性,具體屬性如下表:


tkinter綁定事件

tkinter綁定事件,就是定義一個(gè)函數(shù),然后通過command屬性傳入函數(shù)名,下面通過Button綁定事件,點(diǎn)擊就出現(xiàn)Runsen愛學(xué)習(xí)

  1. from tkinter import * 
  2.  
  3. def p_label(): 
  4.     global root 
  5.     Lb = Label(root, text='Runsen愛學(xué)習(xí)'
  6.     Lb.pack() 
  7.  
  8. root = Tk() 
  9. root.title("應(yīng)用程序窗口"
  10. B_n = Button(root, text='點(diǎn)我', command=p_label, bg='red')  # command后面不能有任何的標(biāo)點(diǎn)符號(hào) 
  11. B_n.pack() 
  12. root.mainloop() 

 

布局顯示

一個(gè)窗口都應(yīng)該有布局,就是pack的時(shí)候需要設(shè)置side,expand需要擴(kuò)展嗎,fill需要填充嗎

  1. from tkinter import * 
  2. root = Tk() 
  3. root.title("應(yīng)用程序窗口"
  4. Button(root,text='1').pack(side=LEFT,expand=YES,fill=Y) 
  5. Button(root,text='2').pack(side=TOP,expand=YES,fill=BOTH) 
  6. Button(root,text='3').pack(side=RIGHT,expand=YES,fill=NONE) 
  7. Button(root,text='4').pack(side=LEFT,expand=NO,fill=Y) 
  8. Button(root,text='5').pack(side=TOP,expand=YES,fill=BOTH) 
  9. Button(root,text='6').pack(side=BOTTOM,expand=YES) 
  10. Button(root,text='7').pack(anchor=SE) 
  11. root.mainloop() 

 

除了pack還有一個(gè)grid,grid將組件布局為表格

下面做一個(gè)電話撥號(hào)盤GUI

  1. from tkinter import * 
  2. root = Tk() 
  3. labels = [['1','2','3'], # 文本,布局為網(wǎng)格 
  4.           ['4','5','6'], 
  5.           ['7','8','9'], 
  6.           ['*','0','#']] 
  7.  
  8. for r in range(4): # 行循環(huán) 
  9.     for c in range(3): # 列循環(huán) 
  10.         label = Label(root, 
  11.                       relief=RAISED, # 設(shè)置邊框格式 
  12.                       padx=10, # 加寬標(biāo)簽 
  13.                       text=labels[r][c]) # 標(biāo)簽文本 
  14.         label.grid(row=r, column=c) # 將標(biāo)簽放置在r行c列 
  15. root.mainloop() 

 

制作一個(gè)日歷

上面教你做一個(gè)電話撥號(hào)盤GUI,下面能做一個(gè)簡單的日歷嗎?

我看你就不會(huì),不是我瞧不起你

放心,有我在。這需要導(dǎo)入calendar模塊了,


  1. import calendar 
  2. from tkinter import * 
  3. root = Tk() 
  4. labels = [['Mon','Tue','Wed','Thu','Fri','Sat','Sun']] 
  5.  
  6. MonthCal = calendar.monthcalendar(2020, 5)  
  7. for i in range(len(MonthCal)): 
  8.     labels.append(MonthCal[i]) 
  9. for r in range(len(MonthCal)+1): 
  10.     for c in range(7): 
  11.         if labels[r][c] == 0: 
  12.             labels[r][c] = ' ' 
  13.         label = Label(root,           
  14.                       padx=5, 
  15.                       pady=5, 
  16.                       text=str(labels[r][c]))         
  17.         label.grid(row=r,column=c) 
  18. root.mainloop() 

 

豐富我們的日歷

上面的日歷就是一個(gè)辣雞,啥功能都沒有,需求很簡單,就是來兩個(gè)按鈕實(shí)現(xiàn)向上翻,向下翻。

向上翻,向下翻兩個(gè)按鈕就需要清空界面,再把日歷加到labels列表中 ,放置日歷。好像很簡單,其實(shí)就是這么簡單。

大家想一想,怎么做出來。我還是給標(biāo)準(zhǔn)實(shí)現(xiàn)代碼

  1. # @Author:Runsen 
  2. import calendar  
  3. from tkinter import * 
  4. root = Tk() 
  5.  
  6.  
  7. def LabelCal(YearMonth): 
  8.     # 首行放置“年、月”的位置 
  9.     label = Label(root,text=str(Year)+"年"
  10.     label.grid(row=0,column=2) 
  11.     label = Label(root,text=str(Month)+"月"
  12.     label.grid(row=0,column=4) 
  13.     # labels列表:放置“星期”的標(biāo)題 
  14.     labels = [['Mon','Tue','Wed','Thu','Fri','Sat','Sun']] 
  15.     # 用calendar庫計(jì)算日歷 
  16.     MonthCal = calendar.monthcalendar(YearMonth
  17.     # 先把界面清空 
  18.     for r in range(7): 
  19.         for c in range(7):             
  20.             label = Label(root, 
  21.                           width =5, 
  22.                           padx=5, 
  23.                           pady=5, 
  24.                           text=' ')         
  25.             label.grid(row=r+1,column=c) 
  26.     # 把日歷加到labels列表中      
  27.     for i in range(len(MonthCal)): 
  28.         labels.append(MonthCal[i]) 
  29.     # 放置日歷 
  30.     for r in range(len(MonthCal)+1): 
  31.         for c in range(7): 
  32.             if labels[r][c] == 0: 
  33.                 labels[r][c] = ' ' 
  34.             label = Label(root, 
  35.                           width =5, 
  36.                           padx=5, 
  37.                           pady=5, 
  38.                           text=str(labels[r][c]))         
  39.             label.grid(row=r+1,column=c) # 網(wǎng)格布局 
  40.  
  41.  
  42. # 默認(rèn)日期 
  43. YearMonth = 2020,5 
  44. LabelCal(YearMonth
  45.          
  46. # button:Enter 
  47. def ButtonPrevious(): 
  48.     global YearMonth 
  49.     Month = Month-1 
  50.     if Month<1: 
  51.         Month = Month+12 
  52.         Year = Year-1 
  53.     LabelCal(YearMonth
  54.      
  55. button1 = Button(root, text='Previous', command=ButtonPrevious) 
  56. button1.grid(row=len(MonthCal)+3, column=0) 
  57.  
  58.  
  59. # button:Clear 
  60. def ButtonNext(): 
  61.     global YearMonth 
  62.     Month = Month+1 
  63.     if Month>12: 
  64.         Month = Month-12 
  65.         Year = Year+1  
  66.     LabelCal(YearMonth
  67.      
  68. button2 = Button(root, text='Next', command=ButtonNext) 
  69. button2.grid(row=len(MonthCal)+3, column=6) 
  70.  
  71. root.mainloop() 

運(yùn)行一波,來一個(gè)最終實(shí)現(xiàn)gif效果圖。

 

責(zé)任編輯:姜華 來源: Python之王
相關(guān)推薦

2020-12-07 12:05:07

GUITkinterPython

2023-05-09 08:24:13

PythonTkinterGUI編程

2023-11-30 15:02:34

Python

2023-11-27 19:42:56

Python GUI編程

2023-11-23 13:10:24

Python框架

2025-03-24 10:55:53

2022-01-07 10:13:07

Tkinter代碼Kivy

2023-09-08 07:54:01

TkinterPython

2024-04-15 16:14:57

2013-06-19 08:52:48

Unity3D

2022-12-07 09:01:14

布局容器VStack?

2025-02-18 08:30:00

GUIPythontkinter

2023-11-29 07:30:08

Python用戶界面

2021-11-17 08:11:35

MySQL

2023-11-09 11:56:28

MySQL死鎖

2021-08-31 07:54:24

SQLDblink查詢

2024-04-26 00:00:00

Rust檢查器代碼

2024-05-07 14:40:49

Python兒童計(jì)算器

2011-06-14 14:57:06

QT Python GUI

2021-03-08 00:11:02

Spring注解開發(fā)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)