Python-tkinter


tkinter

tkinter 是 Python 的標准 GUI 庫。Python 使用 tkinter 可以快速的創建 GUI 應用程序。由於 tkinter 是內置到 python 的安裝包中、只要安裝好 Python 之后就能 import tkinter 庫、而且 IDLE 也是用 tkinter 編寫而成、對於簡單的圖形界面 tkinter 還是能應付自如。

PS:

The package Tkinter has been renamed to tkinter in Python 3, as well as other modules related to it. Here are the name changes:

Tkinter → tkinter
tkMessageBox → tkinter.messagebox:用於顯示在應用程序的消息框
tkColorChooser → tkinter.colorchooser
tkFileDialog → tkinter.filedialog:彈出文件選擇框
tkCommonDialog → tkinter.commondialog
tkSimpleDialog → tkinter.simpledialog
tkFont → tkinter.font
Tkdnd → tkinter.dnd
ScrolledText → tkinter.scrolledtext
Tix → tkinter.tix
ttk → tkinter.ttk

常用控件:

說明:

1、Button

t = Button(master, option=value, ...)

master:按鈕的父容器

option:可選,該按鈕的可設置屬性。

 舉栗子:

import tkinter
import tkinter.messagebox

top = tkinter.Tk()

def hello():
    tkinter.messagebox.showinfo("hello Python","welcome here")

b = tkinter.Button(top,text = "click me", command = hello)
#將小部件放置到主窗口中 b.pack()
#進入消息循環
top.mainloop()

輸出結果截圖:

   

    點擊按鈕后彈出對話框

   

2、Entry(文本框)

用來讓用戶輸入一行文本字符串。

如果需要輸入多行文本,可以使用 Text 組件。

如果需要顯示一行或多行文本且不允許用戶修改,你可以使用 Label 組件。

t = Entry(master,option, ...)

文本框組件常用方法:

 

舉栗子:

from tkinter import *

top = Tk()

L1 = Label(top, text = "姓名:")
L1.pack(side = LEFT)
E1 = Entry(top, bd=5)
E1.pack(side = RIGHT)
 
top.mainloop()

結果截圖:

from tkinter import *

top = Tk()

number = ['0','1','2','3','4']
letter = ['a','b','c','d','e']

#創建兩個列表組件
listbox1 = Listbox(top)
listbox2 = Listbox(top)

for item in number:
    listbox1.insert(0,item)
    
for item in letter:
    listbox2.insert(0,item)

listbox1.pack()
listbox2.pack()
top.mainloop()

結果截圖:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM