闲着无聊为男朋友设置了一个小代码,主要涉及到listbox组件和frame布局。最后的运行出来就是这个样子了。以后直接甩一个程序就好了
listbox的用法:
#coding:utf-8
import tkinter #引入界面设计库
import sys #引入系统交互操作
import os #方便路径访问
import re #引入正则化
#利用此函数实现资源路径的定位
def get_resources_path(relative_path):
if getattr (sys, "frozen", False):
base_path = sys._MEIPASS #获取临时资源
else:
base_path = os.path.abspath(".") #获取当前路径
return os.path.join(base_path, relative_path) #获取绝对路径
LOGO_PATH = get_resources_path(os.path.join("resources" ,"cc1.ico"))
IMAGE_PATH = get_resources_path(os.path.join("resources" ,"cc2.png"))
class MainForm:
def __init__(self):
self. root = tkinter.Tk() #新建一个窗体
self.root.title ( "你们的关系是:") #标题
self.root.geometry("400x300") #初始窗口大小,为小写x
self.root.maxsize (1000,1000) #最大化后窗口大小
self.root.iconbitmap(LOGO_PATH) #图标,bmp格式可直接修改后缀
self.root["background"] = "green" #界面颜色设置,其他颜色可以百度
self.src_listbox()#显示列表
self.but_frame()#显示按钮框架
self.dest_listbox()#显示列表
self.root.mainloop() #显示整个界面
#选择关系列表事件
def src_listbox(self):
#标签
self.relation_label = tkinter.Label (self.root, text = "请做出您的选择", bg = "green", fg = "red", font = ("微软雅黑",20) )
self.relation_label.grid (row =0, column = 0) #标签布局
#关系选择列表申明
self.relation_list = ["路人甲乙丙丁", "朋友", "同事", "亲戚", "表面兄弟","暗恋对象", "恋人" ]
self.relation_listbox = tkinter.Listbox(self.root, selectmode = "single")
#初始化列表信息
for item in self.relation_list:
self.relation_listbox.insert("end", item)
#绑定双击列表响应
self.relation_listbox.bind("<Double-Button-1>", self.change_item_handle)
self.relation_listbox.grid (row =1, column =0) #列表布局
#关系结果列表
def dest_listbox(self):
#关系结果标签
self.choose_label = tkinter.Label(self.root, text = "关系列表", bg = "green", fg = "red", font = ("微软雅黑",20) )
self.choose_label.grid(row =0, column =3) #标签布局
#申明结果列表
self.choose_listbox = tkinter.Listbox(self.root, selectmode = "single")
#绑定双击响应事件
self.choose_listbox.bind("<Double-Button-1>", self.delete_item_handle)
self.choose_listbox.grid(row =1, column =3) #列表布局
#添加按钮函数
def oper_button(self):
#申明添加按钮,在button_frame中
self.add_button = tkinter.Button(self.button_frame, text = "添加>>>", fg = "black", font = ("微软雅黑", 9))
#绑定点击按钮事件
self.add_button.bind ("<Button-1>", self. change_item_handle)
self.add_button.grid(row = 0, column =0) #按钮布局
#添加删除按钮
def del_button(self):
#申明删除按钮,在button_frame中
self.del_button = tkinter.Button(self.button_frame, text = "<<<删除", fg = "black", font = ("微软雅黑", 9))
#绑定点击按钮事件
self.del_button.bind ("<Button-1>", self. delete_item_handle)
self.del_button.grid(row = 1, column =0) #按钮布局
#按钮框
def but_frame(self):
#申明按钮框
self.button_frame = tkinter.Frame(self.root, width = 30)
#为这个框架申明一个列表
self.button_list = []
#调用添加和删除按钮函数
self.oper_button()
self.del_button()
#将按钮们添加到框中
self.button_list.append(self.add_button)
self.button_list.append(self.del_button)
#框布局
self.button_frame.grid(row =1, column = 1)
#左边列表响应函数
def change_item_handle(self, event):
#对于左边列表每一个选中的选项,添加到右边的列表中
for index in self.language_listbox.curselection():
self.choose_listbox.insert("end", self.language_listbox.get(index))
#删除已选中的列表选项
while True:
if self.language_listbox.curselection():
self.language_listbox.delete( self.language_listbox.curselection()[0])
else:
break
#右边列表响应函数
def delete_item_handle(self, event):
for index in self.choose_listbox.curselection():
self.language_listbox.insert("end", self.choose_listbox.get(index))
while True:
if self.choose_listbox.curselection():
self.choose_listbox.delete(self.choose_listbox.curselection()[0])
else:
break
def main():
MainForm()
if __name__ == "__main__":
main()