Python程序設計之GUI(6)

1.使用標準庫來實現圖形化界面

①創建一個登錄程序
a)引入標準庫並初始化應用程序窗口

import tkinter
import tkinter.messagebox

#創建應用程序窗口
root = tkinter.Tk()
#初始化用戶名和密碼文本框內容
varName = tkinter.StringVar()
varName.set('')
varPwd = tkinter.StringVar()
varPwd.set('')

b)創建標籤、文本框

#創建標籤
labelName = tkinter.Label(root, text='User Name:', justify=tkinter.RIGHT, width=80)
#將標籤放到窗口上
labelName.place(x=10, y=5, width=80, height=20)
#創建文本框,同時設置關聯的變量
entryName = tkinter.Entry(root, width=80,textvariable=varName)
entryName.place(x=100, y=5, width=80, height=20)

labelPwd = tkinter.Label(root, text='User Pwd:', justify=tkinter.RIGHT, width=80)
labelPwd.place(x=10, y=30, width=80, height=20)
#創建密碼文本框
entryPwd = tkinter.Entry(root, show='*',width=80, textvariable=varPwd)
entryPwd.place(x=100, y=30, width=80, height=20)

c)按鈕事件以及按鈕組件

#登錄按鈕事件處理函數
def login():
    #獲取用戶名和密碼
    name = entryName.get()
    pwd = entryPwd.get()
    if name=='admin' and pwd=='123456':
        tkinter.messagebox.showinfo(title='Python tkinter',message='OK')
    else:
        tkinter.messagebox.showerror('Python tkinter', message='Error')

#創建按鈕組件,同時設置按鈕事件處理函數
buttonOk = tkinter.Button(root, text='Login', command=login)
buttonOk.place(x=30, y=70, width=50, height=20)

#取消按鈕的事件處理函數
def cancel():
    #清空用戶輸入的用戶名和密碼
    varName.set('')
    varPwd.set('')
buttonCancel = tkinter.Button(root, text='Cancel', command=cancel)
buttonCancel.place(x=90, y=70, width=50, height=20)

d)源代碼

import tkinter
import tkinter.messagebox

#創建應用程序窗口
root = tkinter.Tk()
varName = tkinter.StringVar()
varName.set('hello')
varPwd = tkinter.StringVar()
varPwd.set('')
#創建用戶名標籤
labelName = tkinter.Label(root, text='User Name:', justify=tkinter.RIGHT, width=80)
#將標籤放到窗口上
labelName.place(x=10, y=5, width=80, height=20)
#創建文本框,同時設置關聯的變量
entryName = tkinter.Entry(root, width=80,textvariable=varName)
entryName.place(x=100, y=5, width=80, height=20)

#創建密碼標籤
labelPwd = tkinter.Label(root, text='User Pwd:', justify=tkinter.RIGHT, width=100)
#設置位置
labelPwd.place(x=10, y=30, width=80, height=20)
#創建密碼文本框
entryPwd = tkinter.Entry(root, show='*',width=80, textvariable=varPwd)
entryPwd.place(x=100, y=30, width=80, height=20)

#登錄按鈕事件處理函數
def login():
    #獲取用戶名和密碼
    name = entryName.get()
    pwd = entryPwd.get()
    if name=='admin' and pwd=='123456':
        tkinter.messagebox.showinfo(title='Python tkinter',message='OK')
    else:
        tkinter.messagebox.showerror('Python tkinter', message='Error')

#創建按鈕組件,同時設置按鈕事件處理函數
buttonOk = tkinter.Button(root, text='Login', command=login)
buttonOk.place(x=30, y=70, width=50, height=20)

#取消按鈕的事件處理函數
def cancel():
    #清空用戶輸入的用戶名和密碼
    varName.set('')
    varPwd.set('')
buttonCancel = tkinter.Button(root, text='Cancel', command=cancel)
buttonCancel.place(x=90, y=70, width=50, height=20)

#啓動消息循環
if __name__=="__main__":
    root.mainloop()

e)實現效果
在這裏插入圖片描述
②選擇類組件應用
a)創建應用程序並添加部件

import tkinter
import tkinter.messagebox
import tkinter.ttk

#創建tkinter應用程序
root = tkinter.Tk()
#設置窗口標題
root.title('Selection windows')
#定義窗口大小
root['height'] = 400
root['width'] = 320
#與姓名關聯的變量
varName = tkinter.StringVar()
varName.set('')

b)創建文本框

#創建標籤,然後放到窗口上
labelName = tkinter.Label(root, text='Name:',justify=tkinter.RIGHT,width=50)
labelName.place(x=10, y=5, width=50, height=20)
#創建文本框,同時設置關聯的變量
entryName = tkinter.Entry(root, width=120,textvariable=varName)
entryName.place(x=70, y=5, width=120, height=20)

labelGrade = tkinter.Label(root, text='Grade:', justify=tkinter.RIGHT, width=50)
labelGrade.place(x=10, y=40, width=50, height=20)
#模擬學生所在年級,字典鍵爲年級,字典值爲班級
studentClasses = {'1':['1', '2', '3', '4'],
               '2':['1', '2'],
               '3':['1', '2', '3']}
 #學生年級組合框
comboGrade = tkinter.ttk.Combobox(root,width=50,
                              values=tuple(studentClasses.keys()))
comboGrade.place(x=70, y=40, width=50, height=20)
#綁定組合框事件處理函數
comboGrade.bind('<<ComboboxSelected>>', comboChange)

labelClass = tkinter.Label(root, text='Class:', justify=tkinter.RIGHT, width=50)
labelClass.place(x=130, y=40, width=50, height=20)
#學生年級組合框
comboClass = tkinter.ttk.Combobox(root, width=50)
comboClass.place(x=190, y=40, width=50, height=20)

labelSex = tkinter.Label(root, text='Sex:', justify=tkinter.RIGHT, width=50)
labelSex.place(x=10, y=70, width=50, height=20)
#與性別關聯的變量,1:男;0:女,默認爲男
sex = tkinter.IntVar()
sex.set(1)
#單選鈕,男
radioMan = tkinter.Radiobutton(root,variable=sex,value=1,text='Man')
radioMan.place(x=70, y=70, width=50, height=20)
#單選鈕,女
radioWoman = tkinter.Radiobutton(root,variable=sex,value=0,text='Woman')
radioWoman.place(x=130, y=70, width=70, height=20)
#與是否班長關聯的變量,默認當前學生不是班長
monitor = tkinter.IntVar()
monitor.set(0)
#複選框,選中時變量值爲1,#未選中時變量值爲0
checkMonitor = tkinter.Checkbutton(root,text='Is Monitor?', variable=monitor,
                              onvalue=1, offvalue=0)
checkMonitor.place(x=20, y=100, width=100, height=20)

c)事件處理函數

#事件處理函數
def comboChange(event):
    grade = comboGrade.get()
    if grade:
        #動態改變組合框可選項
        comboClass["values"] = studentClasses.get(grade)
    else:
        comboClass.set([])
#添加按鈕單擊事件處理函數
def addInformation():
    result = 'Name:' + entryName.get()
    result = result + ';Grade:' + comboGrade.get()
    result = result + ';Class:' + comboClass.get()
    result = result + ';Sex:' + ('Man' if sex.get() else 'Woman')
    result = result + ';Monitor:' + ('Yes' if monitor.get() else 'No')
    listboxStudents.insert(0, result)    
buttonAdd = tkinter.Button(root, text='Add',width=40, command=addInformation)
buttonAdd.place(x=130, y=100, width=40, height=20)
#刪除按鈕的事件處理函數
def deleteSelection():
    selection = listboxStudents.curselection()
    if  not selection:
        tkinter.messagebox.showinfo(title='Information', message='No Selection')
    else:
        listboxStudents.delete(selection)
buttonDelete = tkinter.Button(root, text='DeleteSelection',
                        width=100, command=deleteSelection)
buttonDelete.place(x=180, y=100, width=100, height=20)
#創建列表框組件
listboxStudents = tkinter.Listbox(root, width=300)
listboxStudents.place(x=10, y=130, width=300, height=200)        
        

d)源代碼

import tkinter
import tkinter.messagebox
import tkinter.ttk

#創建tkinter應用程序
root = tkinter.Tk()
#設置窗口標題
root.title('Selection windows')
#定義窗口大小
root['height'] = 400
root['width'] = 320
#與姓名關聯的變量
varName = tkinter.StringVar()
varName.set('')
#創建標籤,然後放到窗口上
labelName = tkinter.Label(root, text='Name:',justify=tkinter.RIGHT,width=50)
labelName.place(x=10, y=5, width=50, height=20)
#創建文本框,同時設置關聯的變量
entryName = tkinter.Entry(root, width=120,textvariable=varName)
entryName.place(x=70, y=5, width=120, height=20)

labelGrade = tkinter.Label(root, text='Grade:', justify=tkinter.RIGHT, width=50)
labelGrade.place(x=10, y=40, width=50, height=20)
#模擬學生所在年級,字典鍵爲年級,字典值爲班級
studentClasses = {'1':['1', '2', '3', '4'],
               '2':['1', '2'],
               '3':['1', '2', '3']}
 #學生年級組合框
comboGrade = tkinter.ttk.Combobox(root,width=50,
                              values=tuple(studentClasses.keys()))
comboGrade.place(x=70, y=40, width=50, height=20)
#事件處理函數
def comboChange(event):
    grade = comboGrade.get()
    if grade:
        #動態改變組合框可選項
        comboClass["values"] = studentClasses.get(grade)
    else:
        comboClass.set([])
#綁定組合框事件處理函數
comboGrade.bind('<<ComboboxSelected>>', comboChange)

labelClass = tkinter.Label(root, text='Class:', justify=tkinter.RIGHT, width=50)
labelClass.place(x=130, y=40, width=50, height=20)
#學生年級組合框
comboClass = tkinter.ttk.Combobox(root, width=50)
comboClass.place(x=190, y=40, width=50, height=20)

labelSex = tkinter.Label(root, text='Sex:', justify=tkinter.RIGHT, width=50)
labelSex.place(x=10, y=70, width=50, height=20)
#與性別關聯的變量,1:男;0:女,默認爲男
sex = tkinter.IntVar()
sex.set(1)
#單選鈕,男
radioMan = tkinter.Radiobutton(root,variable=sex,value=1,text='Man')
radioMan.place(x=70, y=70, width=50, height=20)
#單選鈕,女
radioWoman = tkinter.Radiobutton(root,variable=sex,value=0,text='Woman')
radioWoman.place(x=130, y=70, width=70, height=20)
#與是否班長關聯的變量,默認當前學生不是班長
monitor = tkinter.IntVar()
monitor.set(0)
#複選框,選中時變量值爲1,#未選中時變量值爲0
checkMonitor = tkinter.Checkbutton(root,text='Is Monitor?', variable=monitor,
                              onvalue=1, offvalue=0)
checkMonitor.place(x=20, y=100, width=100, height=20)
#添加按鈕單擊事件處理函數
def addInformation():
    result = 'Name:' + entryName.get()
    result = result + ';Grade:' + comboGrade.get()
    result = result + ';Class:' + comboClass.get()
    result = result + ';Sex:' + ('Man' if sex.get() else 'Woman')
    result = result + ';Monitor:' + ('Yes' if monitor.get() else 'No')
    listboxStudents.insert(0, result)    
buttonAdd = tkinter.Button(root, text='Add',width=40, command=addInformation)
buttonAdd.place(x=130, y=100, width=40, height=20)
#刪除按鈕的事件處理函數
def deleteSelection():
    selection = listboxStudents.curselection()
    if  not selection:
        tkinter.messagebox.showinfo(title='Information', message='No Selection')
    else:
        listboxStudents.delete(selection)
buttonDelete = tkinter.Button(root, text='DeleteSelection',
                        width=100, command=deleteSelection)
buttonDelete.place(x=180, y=100, width=100, height=20)
#創建列表框組件
listboxStudents = tkinter.Listbox(root, width=300)
listboxStudents.place(x=10, y=130, width=300, height=200)
#啓動消息循環
if __name__=="__main__":
    root.mainloop()

e)運行結果
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章