300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > python tkinter listbox控件 简书_python tkinter模块的控件操作(1)

python tkinter listbox控件 简书_python tkinter模块的控件操作(1)

时间:2022-02-28 03:57:50

相关推荐

python tkinter listbox控件 简书_python tkinter模块的控件操作(1)

本文中介绍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") #将输入框状态设置为不可写只可读,默认可写

self.tips_text.grid(row=0, column=0)

下拉框

self.number = tk.StringVar()

self.numberChosen = bobox(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.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.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") #获取文本框所有内容

表格

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@

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 = bobox(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@

QQ : 1670829014

Q群 :727588508

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。