class4_Listbox 列表部件
最终的运行效果图:
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# ------------------------------------------------------------
#
# 参考资料:
# 用 python 和 tkinter 做简单的窗口视窗 - 网易云课堂
# http://study.163.com/course/courseLearn.htm?courseId=1003216011#/learn/video?lessonId=1003652323&courseId=1003216011
#
#
# ------------------------------------------------------------
# ******************** class4_Listbox 列表部件 *******************
# ******************** class4_Listbox 列表部件 *******************
# =====>>>>>>内容概览
# =====>>>>>>内容概览
# class4_Listbox'''
# ------------------------------------------------------------
# # 1、 tkinter 之 生成一个窗口
# ------------------------------------------------------------# ------------------------------------------------------------
# # 2、 tkinter 之 窗口 + 标签
# ------------------------------------------------------------# ------------------------------------------------------------
# # 3、 tkinter 之 窗口 + 标签 + 按钮
# ------------------------------------------------------------# ------------------------------------------------------------
# # 4、 tkinter 之 窗口 + 标签 + 按钮 + 产生一个列表
# ------------------------------------------------------------# ------------------------------------------------------------
# # 4.1、 tkinter 之 窗口 + 标签 + 按钮 + 列表(设置内容)
# ------------------------------------------------------------# ------------------------------------------------------------
# # 4.2、 tkinter 之 窗口 + 标签 + 按钮 + 列表(设置, 增加)
# ------------------------------------------------------------# ------------------------------------------------------------
# # 4.3、 tkinter 之 窗口 + 标签 + 按钮 + 列表(设置, 删除)
# ------------------------------------------------------------# ------------------------------------------------------------
# # 5、 tkinter 之 窗口 + 标签(内容) + 按钮(内容) + 列表
# ------------------------------------------------------------# # ------------------------------------------------------------
# # # 6、 tkinter 之 窗口 + 标签(内容) + 按钮(内容) + 列表 + 列表、标签、按钮内容相关联
# # ------------------------------------------------------------
'''
# ------------------------------------------------分割线-------------------------------------------------
# ------------------------------------------------分割线-------------------------------------------------
# ------------------------------------------------分割线-------------------------------------------------
''' # ------------------------------------------------------------ # # 1、 tkinter 之 生成一个窗口 # ------------------------------------------------------------ ''' # # import tkinter as tk # # 生成一个窗口,设置大小 # window = tk.Tk() # window.title("My window") # window.geometry("200x300") # # window.mainloop() # # ------------------------------------------------分割线------------------------------------------------- ''' # ------------------------------------------------------------ # # 2、 tkinter 之 窗口 + 标签 # ------------------------------------------------------------ ''' # # import tkinter as tk # # # 生成一个窗口,设置大小 # window = tk.Tk() # window.title("My window") # window.geometry("200x300") # # # 产生一个标签 # l = tk.Label(window, bg="yellow", width=6) # l.pack() # # window.mainloop() # # ------------------------------------------------分割线------------------------------------------------- ''' # ------------------------------------------------------------ # # 3、 tkinter 之 窗口 + 标签 + 按钮 # ------------------------------------------------------------ ''' # # import tkinter as tk # # # 生成一个窗口,设置大小 # window = tk.Tk() # window.title("My window") # window.geometry("200x300") # # # 产生一个标签 # l = tk.Label(window, bg="yellow", width=6) # l.pack() # # # 生成一个标签 # b = tk.Button(window, text="print selection", width=15, height=2) # b.pack() # # window.mainloop() # ------------------------------------------------分割线------------------------------------------------- ''' # ------------------------------------------------------------ # # 4、 tkinter 之 窗口 + 标签 + 按钮 + 产生一个列表 # ------------------------------------------------------------ ''' # # import tkinter as tk # # # 生成一个窗口,设置大小 # window = tk.Tk() # window.title("My window") # window.geometry("200x300") # # # 产生一个标签 # l = tk.Label(window, bg="yellow", width=6) # l.pack() # # # 生成一个标签 # b = tk.Button(window, text="print selection", width=15, height=2) # b.pack() # # # 生成一个列表 # lb = tk.Listbox(window) # lb.pack() # # window.mainloop() # ------------------------------------------------分割线------------------------------------------------- ''' # ------------------------------------------------------------ # # 4.1、 tkinter 之 窗口 + 标签 + 按钮 + 列表(设置内容) # ------------------------------------------------------------ ''' # # import tkinter as tk # # # 生成一个窗口,设置大小 # window = tk.Tk() # window.title("My window") # window.geometry("200x300") # # # 产生一个标签 # l = tk.Label(window, bg="yellow", width=6) # l.pack() # # # 生成一个标签 # b = tk.Button(window, text="print selection", width=15, height=2) # b.pack() # # # 设置列表的内容 # lb_var = tk.StringVar() # lb_var.set((1111, 2222, 3333, "A", "B", "9999")) # # 生成一个列表 # lb = tk.Listbox(window, listvariable=lb_var ) # listvariable存储列表的内容信息 # lb.pack() # # window.mainloop() # ------------------------------------------------分割线------------------------------------------------- ''' # ------------------------------------------------------------ # # 4.2、 tkinter 之 窗口 + 标签 + 按钮 + 列表(设置, 增加) # ------------------------------------------------------------ ''' # # import tkinter as tk # # # 生成一个窗口,设置大小 # window = tk.Tk() # window.title("My window") # window.geometry("200x300") # # # 产生一个标签 # l = tk.Label(window, bg="yellow", width=6) # l.pack() # # # 生成一个标签 # b = tk.Button(window, text="print selection", width=15, height=2) # b.pack() # # # 设置列表的内容 # lb_var = tk.StringVar() # lb_var.set((1111, 2222, 3333, "A", "B", "9999")) # # 生成一个列表 # lb = tk.Listbox(window, listvariable=lb_var ) # listvariable存储列表的内容信息 # lb.pack() # # # 在列表的内容下添加信息 # list_items = [1,2,3] # for item in list_items: # lb.insert('end',item) # # lb.insert(0,"=============>0") # 位置0, 添加内容 # lb.insert(2,"=============>2") # 位置2, 添加内容 # # window.mainloop() # # # # # ------------------------------------------------分割线------------------------------------------------- ''' # ------------------------------------------------------------ # # 4.3、 tkinter 之 窗口 + 标签 + 按钮 + 列表(设置, 删除) # ------------------------------------------------------------ ''' # # import tkinter as tk # # # 生成一个窗口,设置大小 # window = tk.Tk() # window.title("My window") # window.geometry("200x300") # # # 产生一个标签 # l = tk.Label(window, bg="yellow", width=6) # l.pack() # # # 生成一个标签 # b = tk.Button(window, text="print selection", width=15, height=2) # b.pack() # # # 设置列表的内容 # lb_var = tk.StringVar() # lb_var.set((1111, 2222, 3333, "A", "B", "9999")) # ''' # 列表的内容: # 1111 # 2222 # 3333 # A # B # 9999 # ''' # # 生成一个列表 # lb = tk.Listbox(window, listvariable=lb_var ) # listvariable存储列表的内容信息 # lb.pack() # # # # ==== 删除 ==== # lb.delete(0) # 删除上面的 1111 # lb.delete(0, 1) # 删除上面的 2222 ,3333 # lb.delete(2) # 删除上面的 9999 # # ''' # 列表的内容: # A # B # ''' # # window.mainloop() # # ------------------------------------------------分割线------------------------------------------------- ''' # ------------------------------------------------------------ # # 5、 tkinter 之 窗口 + 标签(内容) + 按钮(内容) + 列表 # ------------------------------------------------------------ ''' # # import tkinter as tk # # # 生成一个窗口,设置大小 # window = tk.Tk() # window.title("My window") # window.geometry("200x300") # # # 设置标签的名字 # l_var = tk.StringVar() # l_var.set( " Here is a Label") # # 产生一个标签 # l = tk.Label(window, bg="yellow", width=20, textvariable=l_var) # l.pack() # # # 设置按钮的名字 # b_var = tk.StringVar() # b_var.set(" Button ") # # # 生成一个按钮 # # b = tk.Button(window, text="print selection", width=15, height=2, textvariable=b_var) # b = tk.Button(window, width=15, height=2, textvariable=b_var) # b.pack() # # # 设置列表的内容 # lb_var = tk.StringVar() # lb_var.set(("A", "B", "C", "D")) # # # 生成一个列表 # lb = tk.Listbox(window, listvariable=lb_var ) # listvariable存储列表的内容信息 # lb.pack() # # window.mainloop() # # # ------------------------------------------------分割线------------------------------------------------- # ''' # # ------------------------------------------------------------ # # # 6、 tkinter 之 窗口 + 标签(内容) + 按钮(内容) + 列表 + 列表、标签、按钮内容相关联 # # ------------------------------------------------------------ # ''' # # import tkinter as tk # # def print_selection(): # ''' # 按钮 ==》触发该函数 ===> 获取列表的内容 ==> 内容储存到value中 ==> value将值传给 Label # :return: # ''' # value = lb.get(lb.curselection()) # 获取列表的中鼠标选中的内容 # l_var.set(value) # value将值传给 Label # # # # # 生成一个窗口,设置大小 # window = tk.Tk() # window.title("My window") # window.geometry("200x300") # # # 设置标签的变量的格式 # l_var = tk.StringVar() # # # 产生一个标签 # l = tk.Label(window, bg="yellow", width=20, textvariable=l_var) # l.pack() # # # # 生成一个按钮 # b = tk.Button(window, text="print selection", width=15, height=2, command=print_selection ) # b.pack() # # # 设置列表的内容 # lb_var = tk.StringVar() # lb_var.set(("A", "B", "C", "D")) # # # 生成一个列表 # lb = tk.Listbox(window, listvariable=lb_var ) # listvariable存储列表的内容信息 # lb.pack() # # window.mainloop()
(序号6程序)最终的运行效果图: