Python之Tkinter使用技巧
使用方法都寫在程序裏面的註釋中,請盡情享用,如果您覺得不錯可以點個贊哦🙂
代碼如下:
"""<<Tkinter16個核心窗口部件>>
Button: 一個簡單的按鈕,用來執行一個命令或別的操作。
Canvas: 組織圖形。這個部件可以用來繪製圖表和圖,創建圖形編輯器,實現定製窗口部件。
Checkbutton: 代表一個變量,它有兩個不同的值。點擊這個按鈕將會在這兩個值間切換。
Entry: 文本輸入域。
Frame: 一個容器窗口部件。幀可以有邊框和背景,當創建一個應用程序或dialog(對話)版面時,幀被用來組織其它的窗口部件。
Label: 顯示一個文本或圖象。
Listbox: 顯示供選方案的一個列表。listbox能夠被配置來得到radiobutton或checklist的行爲。
Menu: 菜單條。用來實現下拉和彈出式菜單。
Menubutton: 菜單按鈕。用來實現下拉式菜單。
Message: 顯示一文本。類似label窗口部件,但是能夠自動地調整文本到給定的寬度或比率。
Radiobutton: 代表一個變量,它可以有多個值中的一個。點擊它將爲這個變量設置值,並且清除與這同一變量相關的其它radiobutton。
Scale: 允許你通過滑塊來設置一數字值。
Scrollbar: 爲配合使用canvas, entry, listbox, and text窗口部件的標準滾動條。
Text: 格式化文本顯示。允許你用不同的樣式和屬性來顯示和編輯文本。同時支持內嵌圖象和窗口。
Toplevel: 一個容器窗口部件,作爲一個單獨的、最上面的窗口顯示。
messageBox: 消息框,用於顯示你應用程序的消息框。(Python2中爲tkMessagebox)
<<grid參數使用方法>>
column: 列數 [number - use cell identified with given column (starting with 0)]
columnspan: 跨列數 [number - this widget will span several columns]
in: master - use master to contain this widget
in_: master - see 'in' option description
ipadx: 單元格左右間距 [amount - add internal padding in x direction]
ipady: 單元格上下間距 [amount - add internal padding in y direction]
padx: 單元格內部元素與單元格的左右間距 [amount - add padding in x direction]
pady: 單元格內部元素與單元格的上下間距 [amount - add padding in y direction]
row: 行數 [number - use cell identified with given row (starting with 0)]
rowspan: 跨行數 [number - this widget will span several rows]
sticky: 空間位置 [NSWE - if cell is larger on which sides will this]
"""
# -*- coding:utf-8 -*-
import tkinter as tk
__author__ = 'Evan'
class GuiSample(object):
def __init__(self):
self.root = tk.Tk()
self.root.title('Gui sample') # 設置GUI標題
# 設置GUI界面屬性
self.root.wm_attributes("-alpha", 1.0) # 設置GUI透明度(0.0~1.0)
self.root.wm_attributes("-topmost", True) # 設置GUI置頂
# self.root.wm_attributes("-toolwindow", True) # 設置爲工具窗口(沒有放大和縮小按鈕)
self.root.overrideredirect(-1) # 去除GUI邊框(GUI標題、放大縮小和關閉按鈕都會消失)
self.bind_window_move_events() # 如果去除GUI邊框了,就要綁定窗口移動事件,否則GUI無法移動和退出
# 設置所有窗口部件
self.build_label()
self.build_entry()
self.build_text()
self.build_check_button()
self.build_button()
# 執行所有窗口部件
self.label.grid(row=0, column=0, sticky=tk.W)
self.entry.grid(row=1, column=0, sticky=tk.W)
self.checkbutton1.grid(row=0, column=1, sticky=tk.W)
self.checkbutton2.grid(row=1, column=1, sticky=tk.W)
self.text_input.grid(row=2, column=0, sticky=tk.W, columnspan=2)
self.quit_button.grid(row=3, column=0, sticky=tk.E)
# 設置窗口位置居中
self.set_gui_geometry(window=self.root)
def build_label(self):
"""
構建文本或圖像控件
:return:
"""
self.label = tk.Label(self.root, text="Label is here")
def build_button(self):
"""
構建點擊按鈕控件
:return:
"""
self.quit_button = tk.Button(self.root, text='Quit', command=self.root.quit, bg='tomato')
def build_check_button(self):
"""
構建多選按鈕控件
:return:
"""
self.checkbutton_a = tk.StringVar()
self.checkbutton1 = tk.Checkbutton(self.root, text="CheckbuttonA", variable=self.checkbutton_a,
offvalue='', onvalue='A') # 選中後variable值爲'A',不選默認爲空字符串
self.checkbutton1.select() # 默認選中這個按鈕
self.checkbutton_b = tk.StringVar()
self.checkbutton2 = tk.Checkbutton(self.root, text="CheckbuttonB", variable=self.checkbutton_b,
offvalue='', onvalue='B') # 選中後variable值爲'B',不選默認爲空字符串
def build_entry(self):
"""
構建單行文本輸入域
:return:
"""
self.entry_input = tk.StringVar()
self.entry = tk.Entry(self.root, textvariable=self.entry_input)
self.entry_input.set('Entry is here') # 寫入信息到Entry
# self.entry_input.get() # 獲取Entry控件內所有信息
# self.entry_input.set('') # 清空Entry控件內所有信息
def build_text(self):
"""
構建多行文本輸入域
:return:
"""
self.text_input = tk.Text(self.root, height=5, width=40)
self.text_input.insert(tk.END, 'Test is here') # 寫入信息到Text
# self.text_input.get(1.0, tk.END).strip() # 獲取Text控件內所有信息
# self.text_input.delete(1.0, tk.END) # 清空Text控件內所有信息
def bind_window_move_events(self):
"""
綁定窗口移動事件
:return:
"""
def start_move(event):
global x, y
x = event.x
y = event.y
def stop_move(event):
global x, y
x = None
y = None
def on_motion(event):
global x, y
deltax = event.x - x
deltay = event.y - y
self.root.geometry("+%s+%s" % (self.root.winfo_x() + deltax, self.root.winfo_y() + deltay))
self.root.update()
self.root.bind("<ButtonPress-1>", start_move) # 監聽左鍵按下操作響應函數
self.root.bind("<ButtonRelease-1>", stop_move) # 監聽左鍵鬆開操作響應函數
self.root.bind("<B1-Motion>", on_motion) # 監聽鼠標移動操作響應函數
@staticmethod
def set_gui_geometry(window, x=2.5, y=4):
"""
設置window的幾何分佈,可以控制x軸和y軸的位置
:param window:
:param x: x軸位置
:param y: y軸位置
:return:
"""
window.update_idletasks()
x_info = (window.winfo_screenwidth() - window.winfo_reqwidth()) / x
y_info = (window.winfo_screenwidth() - window.winfo_reqwidth()) / y
window.geometry('+%d+%d' % (x_info, y_info))
@staticmethod
def set_window_center(window, width=300, height=300):
"""
設置window居中顯示,可以控制窗口的寬度和高度
:param window:
:param width: 寬度
:param height: 高度
:return:
"""
ws = window.winfo_screenwidth()
hs = window.winfo_screenheight()
x = (ws / 2) - (width / 2)
y = (hs / 2) - (height / 2)
window.geometry('%dx%d+%d+%d' % (width, height, x, y))
if __name__ == '__main__':
guiSample = GuiSample()
guiSample.root.mainloop()
執行結果:
我這裏把GUI邊框去除了,看起來非常小巧,配合pyinstaller打包程序食用更佳🙂
移動窗口方法:鼠標左鍵放在窗口任意位置上按住拖動即可😄