1.在tkinter中,Frame是一个容器小部件用于组织和管理其他小部件。它可以作为一个独立的可见区域,也可以作为其他小部件的父容器。
import tkinter as tk
import tkinter.ttk as ttk
import tkinter.messagebox as mbm = tk.Tk()
m.title("tkinter Listbox")
m.geometry('500x300')
m.resizable(0, 0)lb = tk.Label(text='Programming Languages', font=('times', 12, 'bold'), fg='#CD7054')
lb.pack()frame1 = tk.Frame(m)
frame1.pack()varLb = tk.Label(frame1, text='Popular Languages', font=('times', 10, 'bold'), fg='orange')
plotLb = tk.Label(frame1, text='Your Languages', font=('times', 10, 'bold'), fg='orange')
varLb.grid(row=0, column=0, columnspan=3, padx=5)
plotLb.grid(row=0, column=6, columnspan=3, padx=5)lbox = tk.Listbox(frame1, selectmode=tk.EXTENDED, height=8)
lbox.grid(row=1, column=0, columnspan=3, rowspan=5, sticky=tk.E, padx=10)
items = ["c", "c++", "c#", "go", "java", "javascript", "php", "python", "r", "swift"]
for i in items:lbox.insert('end', i) #
list = []
langCnt = 0def Add():cursel = lbox.curselection()curvar = lkLbox.get(0, tk.END)isexist = Falseif len(cursel) > 0:for i in cursel:lkLbox.insert('end', lbox.get(i))if lbox.get(i) in curvar:isexist = Trueif isexist:mb.showinfo('info', 'Add existing language(s)!')def Del():cursel = lkLbox.curselection()if len(cursel) > 0:for i in range(len(cursel) - 1, -1, -1):lkLbox.delete(cursel[i])def Clr():lkLbox.delete(0, tk.END)def Set():global listglobal curCntlist.clear()varturp = lkLbox.get(0, tk.END)if len(varturp) > 0:for var in varturp:list.append(var)else:mb.showwarning('Warning', 'No language is Selected!')langCnt = cntCbox.current()print(list)print(langCnt)btnAdd = tk.Button(frame1, width=6, text='>>', font=('times', 12, 'bold'), fg='green', command=Add)
btnAdd.grid(row=2, column=4, padx=10)btnDel = tk.Button(frame1, width=6, text='Del', font=('times', 12, 'bold'), fg='red', command=Del)
btnDel.grid(row=3, column=4, padx=10)btnClr = tk.Button(frame1, width=6, text='Clr', font=('times', 12, 'bold'), fg='red', command=Clr)
btnClr.grid(row=4, column=4, padx=10)lkLbox = tk.Listbox(frame1, selectmode=tk.EXTENDED, height=8)
lkLbox.grid(row=1, column=6, columnspan=3, rowspan=5, sticky=tk.W, padx=10)frame2 = tk.Frame(m)
frame2.pack(pady='10px')curCntLb = tk.Label(frame2, text='Languages number you master:', font=('times', 10, 'bold'), fg='purple')
curCntLb.grid(row=0, column=0, columnspan=3, padx=5)cntCbox = ttk.Combobox(frame2)
cntCbox['value'] = ('0', '1', '2', '3', '4', '5', '5+')
cntCbox.current(1)
cntCbox.grid(row=0, column=5, columnspan=3, padx=5)btnSet = tk.Button(m, text='Apply', font=('times', 12, 'bold'), fg='blue', command=Set)btnSet.pack(side=tk.BOTTOM, pady=5)
m.mainloop()
2.LabelFrame 控件具有框架功能以及显示标签的功能。默认情况下,它会在其子组件的周围绘制一个边框,并在其上方显示一个文本标题。使用 LabelFrame 可以方便地将多个控件组织在一起,并为它们提供一个视觉上的分组标记。
您提交的内容是:
李白
杜甫
import tkinter as tkdef get_msg():x = ""for j in cheakboxs:# 这里实际上是cheakboxs[j].get() == True# 如果被勾选的话传回来的值为True# 如果没有被勾选的话传回来的值为Falseif cheakboxs[j].get():x = x + persons[j] + "\n"print('您提交的内容是: ')print(x)
# 创建主窗口
root = tk.Tk()# LabelFrame标签框架
labelFrame = tk.LabelFrame(root, text='选择最喜欢的诗人')
persons = {0: '李白', 1: '杜甫', 2: '李清照', 3: '苏轼', 4: '辛弃疾'}
# 这里负责给予字典的键一个False或者True的值,用于检测是否被勾选
cheakboxs = {}
for i in range(len(persons)):# 这里相当于是{0: False, 1: False, 2: False, 3: False, 4: False}cheakboxs[i] = tk.BooleanVar()# 只有被勾选才变为Truetk.Checkbutton(labelFrame, text=persons[i], variable=cheakboxs[i]).grid(row=i + 1, sticky=tk.W)labelFrame.pack(padx=10, pady=5, ipadx=5, ipady=5)
button = tk.Button(root, text="提交", width=10, command=get_msg)
button.pack()
root.mainloop()
示例2
# @Author : 小红牛
# 微信公众号:WdPython
import tkinter
# 创建主窗口
root = tkinter.Tk()
textFirst = "欢迎登录"
photo = tkinter.PhotoImage(file='1.png')
Logo = tkinter.Label(root, image=photo, text=textFirst, compound=tkinter.BOTTOM)
Logo.pack()# LabelFrame标签框架
labelFrame = tkinter.LabelFrame(root, text="登录框")accountLabel = tkinter.Label(labelFrame, text="账号")
accountLabel.grid(row=0, column=0)accountEntry = tkinter.Entry(labelFrame)
accountEntry.grid(row=0, column=1)passWd = tkinter.Label(labelFrame, text="密码")
passWd.grid(row=1, column=0)passWdEntry = tkinter.Entry(labelFrame, show="*")
passWdEntry.grid(row=1, column=1)labelFrame.pack(padx=10, pady=5, ipadx=5, ipady=5)root.mainloop()
3.Toplevel 是 Tkinter GUI 库中的一个控件,它代表一个独立的顶级窗口。与主窗口(通常是 Tk() 实例)不同,Toplevel 窗口是一个弹出窗口,拥有自己的标题栏、边框等部件,并且可以独立于主窗口进行操作。
# @Author : 小红牛
# 微信公众号:WdPython
import tkinter as tk# 创建主窗口
root = tk.Tk()
root.title("Main Window")
root.geometry('160x80')# 创建一个按钮,点击时打开Toplevel窗口
def open_toplevel():top = tk.Toplevel(root)top.geometry('200x100')top.title("Toplevel Window")label = tk.Label(top, text="This is a toplevel window!")label.pack(pady=20)# 创建按钮并添加到主窗口
button = tk.Button(root, text="Open Toplevel", command=open_toplevel)
button.pack(pady=20)# 启动主循环
root.mainloop()
完毕!!感谢您的收看
----------★★历史博文集合★★----------
我的零基础Python教程,Python入门篇 进阶篇 视频教程 Py安装py项目 Python模块 Python爬虫 Json Xpath 正则表达式 Selenium Etree CssGui程序开发 Tkinter Pyqt5 列表元组字典数据可视化 matplotlib 词云图 Pyecharts 海龟画图 Pandas Bug处理 电脑小知识office自动化办公 编程工具