python tkinter模块的控件操作(1)

本文中介绍Python自带GUI模块(Tkinter)控件操作
  1. 导入需要的模块
import tkinter as tk
from tkinter import ttk   
import random,string
windows = tk.Tk()    
windows.title("TKinter表格操作")    #软件打开后显示的名称
  1. 输入框
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)
  1. 下拉框
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)      # 设置其在界面中出现的位置  column代表列   row 代表行
self.numberChosen.current(0)    # 设置下拉列表默认显示的值,0为 numberChosen['values'] 的下标值
  1. 标签
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)
  1. 按钮
self.Login_Button = tk.Button(windows, text="点击自动注册", width=10, height=1, command=self.run)
self.Login_Button.grid(row=0, column=2)
  1. 文本框
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")      #获取文本框所有内容
  1. 表格
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="注册状态")
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)      # 设置其在界面中出现的位置  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="注册状态")
        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()

以上代码可用,还有些控件等下期更新,更新时间(随缘),如果有不懂的地方也可以问我,一下是联系方式:

Email  : soliton.wang@gmail.com
QQ     : 1670829014
Q群   :727588508
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容