找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 956|回复: 1

[求助] 多选下拉列表增加纵向滚动条

2

主题

3

帖子

3

积分

贫民

积分
3
cxnet 发表于 2021-12-29 12:16:35 | 显示全部楼层 |阅读模式
网上看到一个多选下拉列表示范,想给它加一个纵向滚动条,一直没弄成功,请高手指点一下

demo.py文件

  1. from tkinter import *
  2. from ComBoPicker import Combopicker# 导入自定义下拉多选框
  3. if __name__ == "__main__":
  4.    

  5.     root = Tk()
  6.     root.geometry("200x200")

  7.     main =Frame(root)
  8.     main.pack(expand=False, fill="both")
  9.     COMBOPICKER1 = Combopicker(main, values = ['CELL-S1','CELL-S2','CELL-S3','CELL-S4'])
  10.     COMBOPICKER1.pack(anchor="w")

  11.     root.mainloop()
复制代码
ComBoPicker.py文件
  1. '''
  2.         自定义多选下拉列表
  3. '''
  4. import tkinter.font as tkFont
  5. import tkinter.ttk as ttk
  6. from tkinter import *
  7. class Picker(ttk.Frame):

  8.     def __init__(self, master=None,activebackground='#b1dcfb',values=[],entry_wid=None,activeforeground='black', selectbackground='#003eff', selectforeground='white', command=None, borderwidth=1, relief="solid"):

  9.         self._selected_item = None

  10.         self._values = values

  11.         self._entry_wid = entry_wid

  12.         self._sel_bg = selectbackground
  13.         self._sel_fg = selectforeground

  14.         self._act_bg = activebackground
  15.         self._act_fg = activeforeground

  16.         self._command = command
  17.         ttk.Frame.__init__(self, master, borderwidth=borderwidth, relief=relief)

  18.         self.bind("<FocusIn>", lambda event:self.event_generate('<<PickerFocusIn>>'))
  19.         self.bind("<FocusOut>", lambda event:self.event_generate('<<PickerFocusOut>>'))

  20.         self._font = tkFont.Font()

  21.         self.dict_checkbutton = {}
  22.         self.dict_checkbutton_var = {}
  23.         self.dict_intvar_item = {}

  24.         for index,item in enumerate(self._values):

  25.             self.dict_intvar_item[item] = IntVar()
  26.             self.dict_checkbutton[item] = ttk.Checkbutton(self, text = item, variable=self.dict_intvar_item[item],command=lambda ITEM = item:self._command(ITEM))
  27.             self.dict_checkbutton[item].grid(row=index, column=0, sticky=NSEW)
  28.             self.dict_intvar_item[item].set(0)


  29. class Combopicker(ttk.Entry, Picker):
  30.     def __init__(self, master, values= [] ,entryvar=None, entrywidth=None, entrystyle=None, **elect=None,activebackground='#b1dcfb', activeforeground='black', selectbackground='#003eff', selectforeground='white', borderwidth=1, relief="solid"):

  31.         if entryvar is not None:
  32.             self.entry_var = entryvar
  33.         else:
  34.             self.entry_var = StringVar()

  35.         entry_config = {}
  36.         if entrywidth is not None:
  37.             entry_config["width"] = entrywidth

  38.         if entrystyle is not None:
  39.             entry_config["style"] = entrystyle

  40.         ttk.Entry.__init__(self, master, textvariable=self.entry_var, **entry_config, state = "readonly")

  41.         self._is_menuopti**_visible = False

  42.         self.picker_frame = Picker(self.winfo_toplevel(), values=values,entry_wid = self.entry_var,activebackground=activebackground, activeforeground=activeforeground, selectbackground=selectbackground, selectforeground=selectforeground, command=self._on_selected_check)

  43.         self.bind_all("<1>", self._on_click, "+")

  44.         self.bind("<Escape>", lambda event: self.hide_picker())

  45.     @property
  46.     def current_value(self):
  47.         try:
  48.             value = self.entry_var.get()
  49.             return value
  50.         except ValueError:
  51.             return None

  52.     @current_value.setter
  53.     def current_value(self, INDEX):
  54.         self.entry_var.set(values.index(INDEX))

  55.     def _on_selected_check(self, SELECTED):

  56.         value = []
  57.         if self.entry_var.get() != "" and self.entry_var.get() != None:
  58.             temp_value = self.entry_var.get()
  59.             value = temp_value.split(",")

  60.         if str(SELECTED) in value:
  61.             value.remove(str(SELECTED))

  62.         else:   
  63.             value.append(str(SELECTED))

  64.         value.sort()

  65.         temp_value = ""
  66.         for index,item in enumerate(value):
  67.             if item!= "":
  68.                 if index != 0:
  69.                     temp_value += ","
  70.                 temp_value += str(item)

  71.         self.entry_var.set(temp_value)

  72.     def _on_click(self, event):
  73.         str_widget = str(event.widget)

  74.         if str_widget == str(self):
  75.             if not self._is_menuopti**_visible:
  76.                 self.show_picker()
  77.         else:
  78.             if not str_widget.startswith(str(self.picker_frame)) and self._is_menuopti**_visible:
  79.                 self.hide_picker()

  80.     def show_picker(self):
  81.         if not self._is_menuopti**_visible:
  82.             self.picker_frame.place(in_=self, relx=0, rely=1, relwidth=1 )
  83.             self.picker_frame.lift()

  84.         self._is_menuopti**_visible = True

  85.     def hide_picker(self):
  86.         if self._is_menuopti**_visible:
  87.             self.picker_frame.place_forget()

  88.         self._is_menuopti**_visible = False

复制代码


回复

使用道具 举报

2

主题

3

帖子

3

积分

贫民

积分
3
cxnet  楼主| 发表于 2022-1-4 13:13:02 | 显示全部楼层
有人知道吗?请指点一下呢
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表