本文中介紹Python
自帶GUI模塊(Tkinter)控件操作
- 導入需要的模塊
import tkinter as tk
from tkinter import ttk
import random,string
windows = tk.Tk()
windows.title("TKinter表格操作") #軟件打開后顯示的名稱
- 輸入框
self.tips_text = tk.Entry(windows, width=25, font=12, bd=7)
self.tips_text.insert(tk.END, "TKinter表格操作") #向輸入框插入
self.tips_text.configure(state="disabled") #將輸入框狀態(tài)設置為不可寫只可讀,默認可寫
self.tips_text.grid(row=0, column=0)
- 下拉框
self.number = tk.StringVar()
self.numberChosen = ttk.Combobox(windows, width=8, textvariable=self.number, height=3)
self.numberChosen['values'] = ("name", "sex", "classroom") # 設置下拉列表的值
self.numberChosen.grid(column=1, row=0) # 設置其在界面中出現(xiàn)的位置 column代表列 row 代表行
self.numberChosen.current(0) # 設置下拉列表默認顯示的值洛勉,0為 numberChosen['values'] 的下標值
- 標簽
self.status = tk.StringVar()
self.status.set("注冊成功: {}個".format(self.a)) #設置標簽內(nèi)容
self.lblStatus = tk.Label(windows, textvariable=self.status, anchor='c')
self.lblStatus.grid(row=2, column=1, columnspan=20, sticky=tk.S + tk.E)
- 按鈕
self.Login_Button = tk.Button(windows, text="點擊自動注冊", width=10, height=1, command=self.run)
self.Login_Button.grid(row=0, column=2)
- 文本框
self.text = tk.StringVar()
self.text = tk.Text(windows, width=75, height=20,)
self.text.grid(row=1, column=0, columnspan=20, rowspan=2)
self.text.get(1.0, "end") #獲取文本框所有內(nèi)容
- 表格
self.columns = ("phone", "password", "state")
self.tables = ttk.Treeview(windows, show="headings", columns=self.columns, selectmode=tk.BROWSE)
self.tables.column("phone", anchor="center", width=130)
self.tables.column("password", anchor="center", width=150)
self.tables.column("state", anchor="center", width=100)
self.tables.heading("phone", text="手機號")
self.tables.heading("password", text="密碼")
self.tables.heading("state", text="注冊狀態(tài)")
self.tables.grid(row=1, column=0, columnspan=3)
整體代碼
# coding : utf-8
"""
Author : soliton
Email : soliton.wang@gmail.com
QQ : 1670829014
"""
import tkinter as tk
from tkinter import ttk
import random,string
windows = tk.Tk()
# 設置圖標
windows.iconbitmap('favicon.ico')
windows.title("TKinter表格操作")
class Display:
def __init__(self):
self.a = 0
self.b = 0
self.c = 0
self.insert_execl = []
self.tips_text = tk.Entry(windows, width=25, font=12, bd=7)
self.tips_text.insert(tk.END, "TKinter表格操作")
self.tips_text.configure(state="disabled")
self.tips_text.grid(row=0, column=0)
self.number = tk.StringVar()
self.numberChosen = ttk.Combobox(windows, width=8, textvariable=self.number, height=3)
self.numberChosen['values'] = ("name", "sex", "classroom") # 設置下拉列表的值
self.numberChosen.grid(column=1, row=0) # 設置其在界面中出現(xiàn)的位置 column代表列 row 代表行
self.numberChosen.current(0) # 設置下拉列表默認顯示的值杨幼,0為 numberChosen['values'] 的下標值
self.columns = ("phone", "password", "state")
self.tables = ttk.Treeview(windows, show="headings", columns=self.columns, selectmode=tk.BROWSE)
self.tables.column("phone", anchor="center", width=130)
self.tables.column("password", anchor="center", width=150)
self.tables.column("state", anchor="center", width=100)
self.tables.heading("phone", text="手機號")
self.tables.heading("password", text="密碼")
self.tables.heading("state", text="注冊狀態(tài)")
self.tables.grid(row=1, column=0, columnspan=3)
self.status = tk.StringVar()
self.status.set("注冊成功: {}個".format(self.a))
self.lblStatus = tk.Label(windows, textvariable=self.status, anchor='c')
self.lblStatus.grid(row=2, column=1, columnspan=20, sticky=tk.S + tk.E)
self.status_error = tk.StringVar()
self.status_error.set("注冊失敗: {}個".format(self.b))
self.lblStatus_error = tk.Label(windows, textvariable=self.status_error, anchor='c')
self.lblStatus_error.grid(row=2, column=0, columnspan=20, sticky=tk.S)
self.Login_Button = tk.Button(windows, text="點擊自動注冊", width=10, height=1, command=self.run)
self.Login_Button.grid(row=0, column=2)
def run(self):
self.password = ''.join(random.sample(string.ascii_lowercase, 8))
self.username = ''.join(random.sample(string.ascii_uppercase, 5))
self.insert_execl.clear()
self.insert_execl.append(self.username)
self.insert_execl.append(self.password)
self.insert_execl.append('注冊成功')
print(self.insert_execl)
if "注冊成功" in self.insert_execl:
self.tables.insert('', self.c,values=("{}".format(self.insert_execl[0]), self.insert_execl[1], self.insert_execl[2]))
self.a += 1
self.c += 1
self.status.set("注冊成功: {}個".format(self.a))
else:
self.tables.insert('', self.c,values=("{}".format(self.insert_execl[0]), self.insert_execl[1], self.insert_execl[2]))
self.b +=1
self.c += 1
self.status_error.set("注冊失敗: {}個".format(self.b))
if __name__ == '__main__':
Display()
windows.mainloop()
以上代碼可用,還有些控件等下期更新复局,更新時間(隨緣),如果有不懂的地方也可以問我,一下是聯(lián)系方式:
Email : soliton.wang@gmail.com
QQ : 1670829014
Q群 :727588508