python GUI(Tkinter)

Tkinter簡介python

  是python內置的標準GUI庫,在安裝python後,導入模塊便可正常使用,Tk和Tkinter可在大多數的Unix,以及Windows和Macintosh系統上運行。app

關於Tkinter的導入(注意大小寫)框架

在2.x版本上,編寫爲:from Tkinter import *oop

在3.x版本上,編寫爲:from tkinter import *佈局

 

Tkinter組件字體

控件 描述                                                 
Button 按鈕控件
Canvas 畫布控件,顯示圖形元素如線條或文本
Checkbutton 多選框控件,不存在互斥,可多個選擇
Entry 輸入控件,用於輸入或者顯示文本內容
Frame 框架控件,在屏幕上顯示一個矩形區,多用於做爲容器
Label 標籤控件,可用於顯示文本和位圖
Listbox 列表控件
Menubutton 菜單項按鈕控件
Menu 菜單控件,顯示菜單欄,下拉菜單和彈出菜單等
Message 消息控件,用於顯示多行文本,與Label相似
Radiobutton 多選框控件,存在互斥,多個只能選擇一個
Scale 範圍控件,顯示一個數值刻度並設定範圍
Scrollbar 滾動條控件,當內容超過可視化區域的時候使用,好比列表框
Text 文本控件,用於顯示多行文本
Toplevel 容器控件,用來提供一個單獨的對話框,和Frame相似
Spinbox 輸入控件,與Entry相似,可是能夠指定輸入範圍值
PanedWindow 窗口布局管理的插件,能夠包含一個或多個子控件
LabelFrame 容器控件,經常使用於負責的窗口布局
tkMessageBox 用於顯示你應用程序的消息框

Tkinter組件的共同屬性
spa

屬性    描述                                           
Dimension 控件大小
Color 控件顏色
Font 控件字體
Anchor 錨點
Relief 控件樣式
Bitmap 位圖
Cursor 光標

Tkinter組件的佈局管理,有三種pack, grid,place.net

關於佈局管理的,推薦網友博客(感謝分享):https://blog.csdn.net/liuxu0703/article/details/54428405插件

關於Grid講解的,推薦網友博客:https://blog.csdn.net/ligou8000/article/details/463310753d

我只簡單的說明grid的幾種屬性,用於你們對下面控件示例代碼的理解。

option           說明                                                         
row 控件擺放的行數值,從0開始,
column 控件擺放的列數值,從0開始
rowspan 可指定控件跨越多行顯示,
column 可指定控件跨越多列顯示,
padx x軸方向的填充,在控件外部的左右部分填充指定長度,數值必須大於0
pady y軸方向的填充,在控件外部的上下部分填充指定長度,數值必須大於0
sticky

控件的對齊方式,默認會居中顯示,可使用的值有:

N/S/E/W,分別表明上,下,左,右。

若是你但願控件左對齊的話,能夠編寫程序: sticky=E

若是你但願控件左上對齊的話,能夠編寫程序: sticky=E+N

Tkinter的使用示例:

首先搭建框架相關

# -*- coding:utf-8 -*-
# __author__ = 'Code~'

import Tkinter                  # 引用Tkinter庫
from Tkinter import *           # 導入Tkinter庫方法

DEF_PATH = 'C:\Python27'
# 消息框模塊
class MsgBox:
    def __init__(self):
        # 建立Tkinter對象
        self.root = Tkinter.Tk()
        # 標題
        self.root.title('Tkinter Demo')
        # 設定窗口大小,注意‘x’必定要小寫,不然TclError: bad geometry specifier "500X400"
        self.root.geometry('500x400')
        # 初始化UI相關
        self.initUI()
        # 消息循環
        self.root.mainloop()
    
    # 初始化控件
    def initUI(self):
        root = self.root   
 

if __name__  == "__main__":
    messageBox = MsgBox()

接下來咱們會將UI控件的示例編寫在方法initUI中,爲了查看方便,會分塊展現:

Button

# text:按鈕標題
# command: 事件綁定,當按鈕被點擊時,會執行
Button(self.root, text='按鈕', command=self.BtnEvent).grid(row=0, column=0)

def BtnEvent(self):
    print(u'您點擊了按鈕')

Checkbutton

self.chkVar = []                    
for i in range(4):
    _str = 'CheckBtn' + str(i)  # 標題
    # 注意,此處不可編寫爲self.chkVar[i] = IntVar()
    # 不然報錯:IndexError: list assignment index out of range
    self.chkVar.append(IntVar())
    # text: 標題
    # variable: 對象整型變量
    # command: 關聯事件
    chkBtn = Checkbutton(self.root,text=_str,variable=self.chkVar[i],command=self.chkEvt)
    chkBtn.grid(row=1, column=(i+1))

# 事件
def chkEvt(self):
    for i in range(4):
       # 獲取選中狀態,若是選中爲1,不然爲0
        index = self.chkVar[i].get()     
        if index > 0:
             print(u'你選擇的按鈕爲:CheckBtn' + str(i))

 

Radiobutton

# 注意此處,可不用使用列表
self.radioVar = IntVar()    
for i in range(1,4):
    _str = 'RaidoBtn' + str(i)
    # text: 標題
    # variable: 對象的整型變量
    # value: 獲取選擇對象的索引
    radioBtn = Radiobutton(self.root,text=_str,variable=self.radioVar,value=i,command=self.radioEvt)
    radioBtn.grid(row=2, column=(i+1))

def radioEvt(self):
        index = self.radioVar.get()
        print(u'你選擇的radioButton的索引爲:' + str(index))

 Label

#Label文本
strContent = "這是標籤控件,屬性:bg設置背景顏色,fg設置文本顏色"
label = Label(self.root, text=strContent,bg='red',fg='blue',height=3)
label.grid(row=0, column=0)     

Entry

def initUI(self):
    #Entry
    # 不可以使用text屬性來設定
    en0 = Entry(self.root, text='請輸入文本...',width=50, bg='red')
    en0.grid(row=1, column=0, sticky='E')
    # 使用StringVar()來綁定Entry,經過set來設定初始化值,經過get來獲取,其輸入值可改變
    self.inputVar = StringVar()
    self.inputVar.set('請輸入內容....')
    en1 = Entry(self.root, textvariable=self.inputVar, width=50,bg='yellow')
    en1.grid(row=2, column=0, sticky='E')
    Button(self.root, text='確認1', command=self.sureEvent1).grid(row=2, column=1)
    # 密碼輸入框
    self.inputVar2 = StringVar()
    self.inputVar2.set('請輸入密碼')
    en2 = Entry(self.root, textvariable=self.inputVar2, width=50,show='*')
    en2.grid(row=3, column=0, sticky='E')
    Button(self.root, text='確認2', command=self.sureEvent2).grid(row=3, column=1)
    # 設置輸入框屬性
    # 屬性值有4個:normal(可寫),disabled(不可操做), readonly(只讀,不可操做),經過state來設定
    self.inputVar3 = StringVar()
    self.inputVar3.set('readonly')
    en3 = Entry(self.root, textvariable=self.inputVar3, width=50,state='readonly')
    en3.grid(row=4, column=0, sticky='E')
    Button(self.root, text='確認3', command=self.sureEvent3).grid(row=4, column=1)
    
def sureEvent1(self):
    inputStr = self.inputVar.get()
    print(u'您的第二個輸入框內容:' + inputStr)

def sureEvent2(self):
    inputStr = self.inputVar2.get()
    print(u'您的第三個輸入框內容:' + inputStr)

def sureEvent3(self):
    inputStr = self.inputVar3.get()
    print(u'您的第四個輸入框內容:' + inputStr)

相關文章
相關標籤/搜索