当前位置:   article > 正文

Python tkinter控件全集之组合选择框 ttk.ComboBox_tkinter多选框

tkinter多选框

Tkinter标准库
Tkinter是Python的标准GUI库,也是最常用的Python GUI库之一,提供了丰富的组件和功能,包括窗口、按钮、标签、文本框、列表框、滚动条、画布、菜单等,方便开发者进行图形界面的开发。Tkinter库基于Tk for Unix/Windows/macOS,由Tcl语言编写。使用Tkinter,可以快速创建桌面应用程序,并支持多平台Windows、macOS、Linux等vb.net教程C#教程python教程

tkinter控件全集
在python中导入tkinter库后,有18种控件(也称组件):

导入方式:import tkinter as tk

Button、Canvas、Checkbutton、Entry、Frame、Label、LabelFrame、Listbox、Menu、Menubutton、Message、OptionMenu、PanedWindow、Radiobutton、Scale、Scrollbar、Spinbox、Text

最学见的按钮、文本框、标签、列表框等都在里边,唯独没看到组合框ComboBox。查过资料后才知道,tkinter库中还有一子模块tkinter.ttk,它包含有包括Combobox在内的20种控件:

导入方式:from tkinter import ttk

Button、Checkbutton、Combobox、Entry、Frame、Label、LabelFrame、LabeledScale、Labelframe、Menubutton、Notebook、OptionMenu、PanedWindow、Progressbar、Radiobutton、Scale、Scrollbar、Separator、Sizegrip、Spinbox、Treeview

请注意,某些控件在两个模块中都存在(如Button, Checkbutton, Entry等),但它们在外观和行为上可能会有所不同,ttk模块中的控件可以提供更加现代和可定制的外观。以Button为例,都以简单的方式表达,运行后的外观和行为都有些不同。

  1. import tkinter as tk
  2. from tkinter import ttk
  3. button1 = tk.Button(root, text="Click Me", command=button_click)
  4. button2 = ttk.Button(root, text="Click Me", command=button_click)

如图所示:

完整代码如下:

  1. import tkinter as tk
  2. from tkinter import ttk
  3. def button_click():
  4. print("Button clicked!")
  5. if __name__=='__main__':
  6. root = tk.Tk()
  7. root.title("Button vs ttk.Button")
  8. root.geometry("400x300")
  9. button1 = tk.Button(root, text="Click Me", command=button_click)
  10. button1.pack(pady=60)
  11. button2 = ttk.Button(root, text="Click Me", command=button_click)
  12. button2.pack(pady=20)
  13. root.mainloop()

以下将介绍今天的主角:ttk.Combobox 组合选择框。

ComboBox 组合选择框
ComboBox 将几个文本字段组合成为一个可弹出的选择列表。它的原版帮助有3万多字呢,以下挑一些主要的操作方法简单介绍一下:

创建并设置选择项

  1. values = ["Option 1", "Option 2", "Option 3", "Option 4"]
  2. combobox = ttk.Combobox(root, values=values)
  3. combobox.pack()

绑定事件ComboboxSelected
combobox.bind("<<ComboboxSelected>>", on_select)

获取当前选择项combobox.get()
combobox.get()

以上插图的代码如下:

  1. import tkinter as tk
  2. from tkinter import ttk
  3. def on_select(event):
  4. label.config(text = "当前选择为:" + combobox.get())
  5. if __name__=='__main__':
  6. # 创建主窗口
  7. root = tk.Tk()
  8. root.title("Combobox Example")
  9. root.geometry("400x300")
  10. label = tk.Label(root, text="请点击下拉框选择:")
  11. label.pack()
  12. # 创建多选下拉框
  13. values = ["Option 1", "Option 2", "Option 3", "Option 4"]
  14. combobox = ttk.Combobox(root, values=values)
  15. combobox.pack()
  16. combobox.bind("<<ComboboxSelected>>", on_select)
  17. # 运行主循环
  18. root.mainloop()

设置当前显示项combobox.set()
combobox.set()

自定义类 SelectCombobox
把以上内容综合起来,自定义一个组合框类:

  1. import tkinter as tk
  2. from tkinter import ttk
  3. class SelectCombobox(ttk.Combobox):
  4. def __init__(self, master=None, values=None):
  5. super().__init__(master, values=values)
  6. self.bind("<<ComboboxSelected>>", self.on_select)
  7. def on_select(self, event):
  8. label.config(text = "当前选择为:" + self.get())
  9. if __name__=='__main__':
  10. # 创建主窗口
  11. root = tk.Tk()
  12. root.title("Combobox Example")
  13. root.geometry("400x300")
  14. label = tk.Label(root, text="请点击下拉框选择:")
  15. label.pack()
  16. # 创建多选下拉框
  17. values = ["Option 1", "Option 2", "Option 3", "Option 4"]
  18. combobox = SelectCombobox(root, values=values)
  19. combobox.pack()
  20. # 运行主循环
  21. root.mainloop()

扩展Combobox组合框
拓展成多项选择
Combobox本身不支持多项选择,变通方法:引入一个列表,用于存放已选项,奇数次选择一个项则存入列表、反之偶数次选择则移除。

代码如下:

  1. import tkinter as tk
  2. from tkinter import ttk
  3. class MultiSelectCombobox(ttk.Combobox):
  4. def __init__(self, master=None, values=None, **kwargs):
  5. super().__init__(master, values=values, **kwargs)
  6. self.bind("<<ComboboxSelected>>", self.on_select)
  7. self.selected_values = []
  8. def on_select(self, event):
  9. selected_value = self.get() # 使用 get 方法获取选中项的值
  10. if selected_value in self.selected_values:
  11. self.selected_values.remove(selected_value) # 如果已选中,则移除该选项
  12. else:
  13. self.selected_values.append(selected_value) # 如果未选中,则添加到已选项列表
  14. self.selected_values.sort()
  15. if self.selected_values:
  16. text = '、'.join(self.selected_values)
  17. else:
  18. text = '空'
  19. label2.config(text = "当前选择为:" + text)
  20. if __name__=='__main__':
  21. # 创建主窗口
  22. root = tk.Tk()
  23. root.title("Multi-Select Combobox Example")
  24. root.geometry("400x300")
  25. label = tk.Label(root, text="请选择:")
  26. label.pack()
  27. # 创建多选下拉框
  28. values = ["Option 1", "Option 2", "Option 3", "Option 4"]
  29. combobox = MultiSelectCombobox(root, values=values)
  30. combobox.pack()
  31. text = "当前选择为:空"
  32. label2 = tk.Label(root, text=text)
  33. label2.pack()
  34. # 运行主循环
  35. root.mainloop()

选项前添加符号
Combobox本身不支持修改选择项,变通方法:在每次变更选项时都重建一个新的组合框,同时把所有的选项前都加上一个全角空格(\u3000),对于已选中的选项,则把前面的全角空格替换成对勾符号(\u2713)。

这样子可能更像一个多项选择框,如下图:

代码如下:

  1. import tkinter as tk
  2. from tkinter import ttk
  3. def on_select(event):
  4. selected_value = combobox.get()
  5. selected_value = selected_value.replace("\u2713","\u3000")
  6. if selected_value in selected_values:
  7. selected_values.remove(selected_value)
  8. else:
  9. selected_values.append(selected_value)
  10. selected_values.sort()
  11. if selected_values:
  12. text = '、'.join(selected_values).replace('tion ','')
  13. else:
  14. text = '空'
  15. label.config(text = "当前选择为:" + text)
  16. update_combobox_options()
  17. def update_combobox_options():
  18. global combobox
  19. combobox.grid_forget()
  20. new_values = []
  21. for v in values:
  22. if v in selected_values:
  23. new_values.append(v.replace("\u3000","\u2713"))
  24. else:
  25. new_values.append(v)
  26. combobox = ttk.Combobox(root, values=new_values, state='readonly')
  27. combobox.place(x=100, y=30)
  28. combobox.set('请点击选择:')
  29. combobox.bind("<<ComboboxSelected>>", on_select)
  30. if __name__=='__main__':
  31. # 创建主窗口
  32. root = tk.Tk()
  33. root.title("Multi-Select Combobox Example")
  34. root.geometry("400x300")
  35. # 创建多选下拉框
  36. values = ["Option 1", "Option 2", "Option 3", "Option 4"]
  37. values = ["\u3000"+v for v in values]
  38. selected_values = []
  39. combobox = ttk.Combobox(root, values=values, state='readonly')
  40. combobox.place(x=100, y=30)
  41. combobox.set('请点击选择:')
  42. combobox.bind("<<ComboboxSelected>>", on_select)
  43. text = "当前选择为:空"
  44. label = tk.Label(root, text=text)
  45. label.pack(side=tk.BOTTOM)
  46. # 运行主循环
  47. root.mainloop()

题外话
以上这些是我自己瞎写的变通代码,可能有更好的办法来实现这些功能,也有可能ttk模块本身就有这些功能,只是我没发现而已。另外:如果选择项不至是四个而是有很多,最好能在最前面设置两个选项,全部选择和取消全选,它们不参与进入已选项列表,点击它们只为在其它选项上标注对勾同时填满或清空已选项列表。感兴趣的朋友,可以自己动手实现这两个功能。

附件一
Combobox 英文帮助
Help on class Combobox in module tkinter.ttk:

  1. class Combobox(Entry)
  2. | Combobox(master=None, **kw)
  3. |
  4. | Ttk Combobox widget combines a text field with a pop-down list of values.
  5. |
  6. | Method resolution order:
  7. | Combobox
  8. | Entry
  9. | Widget
  10. | tkinter.Entry
  11. | tkinter.Widget
  12. | tkinter.BaseWidget
  13. | tkinter.Misc
  14. | tkinter.Pack
  15. | tkinter.Place
  16. | tkinter.Grid
  17. | tkinter.XView
  18. | builtins.object
  19. |
  20. | Methods defined here:
  21. |
  22. | __init__(self, master=None, **kw)
  23. | Construct a Ttk Combobox widget with the parent master.
  24. |
  25. | STANDARD OPTIONS
  26. |
  27. | class, cursor, style, takefocus
  28. |
  29. | WIDGET-SPECIFIC OPTIONS
  30. |
  31. | exportselection, justify, height, postcommand, state,
  32. | textvariable, values, width
  33. |
  34. | current(self, newindex=None)
  35. | If newindex is supplied, sets the combobox value to the
  36. | element at position newindex in the list of values. Otherwise,
  37. | returns the index of the current value in the list of values
  38. | or -1 if the current value does not appear in the list.
  39. |
  40. | set(self, value)
  41. | Sets the value of the combobox to value.
  42. |
  43. | ----------------------------------------------------------------------
  44. | Methods inherited from Entry:
  45. |
  46. | bbox(self, index)
  47. | Return a tuple of (x, y, width, height) which describes the
  48. | bounding box of the character given by index.
  49. |
  50. | identify(self, x, y)
  51. | Returns the name of the element at position x, y, or the
  52. | empty string if the coordinates are outside the window.
  53. |
  54. | validate(self)
  55. | Force revalidation, independent of the conditions specified
  56. | by the validate option. Returns False if validation fails, True
  57. | if it succeeds. Sets or clears the invalid state accordingly.
  58. |
  59. | ----------------------------------------------------------------------
  60. | Methods inherited from Widget:
  61. |
  62. | instate(self, statespec, callback=None, *args, **kw)
  63. | Test the widget's state.
  64. |
  65. | If callback is not specified, returns True if the widget state
  66. | matches statespec and False otherwise. If callback is specified,
  67. | then it will be invoked with *args, **kw if the widget state
  68. | matches statespec. statespec is expected to be a sequence.
  69. |
  70. | state(self, statespec=None)
  71. | Modify or inquire widget state.
  72. |
  73. | Widget state is returned if statespec is None, otherwise it is
  74. | set according to the statespec flags and then a new state spec
  75. | is returned indicating which flags were changed. statespec is
  76. | expected to be a sequence.
  77. |
  78. | ----------------------------------------------------------------------
  79. | Methods inherited from tkinter.Entry:
  80. |
  81. | delete(self, first, last=None)
  82. | Delete text from FIRST to LAST (not included).
  83. |
  84. | get(self)
  85. | Return the text.
  86. |
  87. | icursor(self, index)
  88. | Insert cursor at INDEX.
  89. |
  90. | index(self, index)
  91. | Return position of cursor.
  92. |
  93. | insert(self, index, string)
  94. | Insert STRING at INDEX.
  95. |
  96. | scan_dragto(self, x)
  97. | Adjust the view of the canvas to 10 times the
  98. | difference between X and Y and the coordinates given in
  99. | scan_mark.
  100. |
  101. | scan_mark(self, x)
  102. | Remember the current X, Y coordinates.
  103. |
  104. | select_adjust = selection_adjust(self, index)
  105. |
  106. | select_clear = selection_clear(self)
  107. |
  108. | select_from = selection_from(self, index)
  109. |
  110. | select_present = selection_present(self)
  111. |
  112. | select_range = selection_range(self, start, end)
  113. |
  114. | select_to = selection_to(self, index)
  115. |
  116. | selection_adjust(self, index)
  117. | Adjust the end of the selection near the cursor to INDEX.
  118. |
  119. | selection_clear(self)
  120. | Clear the selection if it is in this widget.
  121. |
  122. | selection_from(self, index)
  123. | Set the fixed end of a selection to INDEX.
  124. |
  125. | selection_present(self)
  126. | Return True if there are characters selected in the entry, False
  127. | otherwise.
  128. |
  129. | selection_range(self, start, end)
  130. | Set the selection from START to END (not included).
  131. |
  132. | selection_to(self, index)
  133. | Set the variable end of a selection to INDEX.
  134. |
  135. | ----------------------------------------------------------------------
  136. | Methods inherited from tkinter.BaseWidget:
  137. |
  138. | destroy(self)
  139. | Destroy this and all descendants widgets.
  140. |
  141. | ----------------------------------------------------------------------
  142. | Methods inherited from tkinter.Misc:
  143. |
  144. | __getitem__ = cget(self, key)
  145. |
  146. | __repr__(self)
  147. | Return repr(self).
  148. |
  149. | __setitem__(self, key, value)
  150. |
  151. | __str__(self)
  152. | Return the window path name of this widget.
  153. |
  154. | after(self, ms, func=None, *args)
  155. | Call function once after given time.
  156. |
  157. | MS specifies the time in milliseconds. FUNC gives the
  158. | function which shall be called. Additional parameters
  159. | are given as parameters to the function call. Return
  160. | identifier to cancel scheduling with after_cancel.
  161. |
  162. | after_cancel(self, id)
  163. | Cancel scheduling of function identified with ID.
  164. |
  165. | Identifier returned by after or after_idle must be
  166. | given as first parameter.
  167. |
  168. | after_idle(self, func, *args)
  169. | Call FUNC once if the Tcl main loop has no event to
  170. | process.
  171. |
  172. | Return an identifier to cancel the scheduling with
  173. | after_cancel.
  174. |
  175. | anchor = grid_anchor(self, anchor=None)
  176. |
  177. | bell(self, displayof=0)
  178. | Ring a display's bell.
  179. |
  180. | bind(self, sequence=None, func=None, add=None)
  181. | Bind to this widget at event SEQUENCE a call to function FUNC.
  182. |
  183. | SEQUENCE is a string of concatenated event
  184. | patterns. An event pattern is of the form
  185. | <MODIFIER-MODIFIER-TYPE-DETAIL> where MODIFIER is one
  186. | of Control, Mod2, M2, Shift, Mod3, M3, Lock, Mod4, M4,
  187. | Button1, B1, Mod5, M5 Button2, B2, Meta, M, Button3,
  188. | B3, Alt, Button4, B4, Double, Button5, B5 Triple,
  189. | Mod1, M1. TYPE is one of Activate, Enter, Map,
  190. | ButtonPress, Button, Expose, Motion, ButtonRelease
  191. | FocusIn, MouseWheel, Circulate, FocusOut, Property,
  192. | Colormap, Gravity Reparent, Configure, KeyPress, Key,
  193. | Unmap, Deactivate, KeyRelease Visibility, Destroy,
  194. | Leave and DETAIL is the button number for ButtonPress,
  195. | ButtonRelease and DETAIL is the Keysym for KeyPress and
  196. | KeyRelease. Examples are
  197. | <Control-Button-1> for pressing Control and mouse button 1 or
  198. | <Alt-A> for pressing A and the Alt key (KeyPress can be omitted).
  199. | An event pattern can also be a virtual event of the form
  200. | <<AString>> where AString can be arbitrary. This
  201. | event can be generated by event_generate.
  202. | If events are concatenated they must appear shortly
  203. | after each other.
  204. |
  205. | FUNC will be called if the event sequence occurs with an
  206. | instance of Event as argument. If the return value of FUNC is
  207. | "break" no further bound function is invoked.
  208. |
  209. | An additional boolean parameter ADD specifies whether FUNC will
  210. | be called additionally to the other bound function or whether
  211. | it will replace the previous function.
  212. |
  213. | Bind will return an identifier to allow deletion of the bound function with
  214. | unbind without memory leak.
  215. |
  216. | If FUNC or SEQUENCE is omitted the bound function or list
  217. | of bound events are returned.
  218. |
  219. | bind_all(self, sequence=None, func=None, add=None)
  220. | Bind to all widgets at an event SEQUENCE a call to function FUNC.
  221. | An additional boolean parameter ADD specifies whether FUNC will
  222. | be called additionally to the other bound function or whether
  223. | it will replace the previous function. See bind for the return value.
  224. |
  225. | bind_class(self, className, sequence=None, func=None, add=None)
  226. | Bind to widgets with bindtag CLASSNAME at event
  227. | SEQUENCE a call of function FUNC. An additional
  228. | boolean parameter ADD specifies whether FUNC will be
  229. | called additionally to the other bound function or
  230. | whether it will replace the previous function. See bind for
  231. | the return value.
  232. |
  233. | bindtags(self, tagList=None)
  234. | Set or get the list of bindtags for this widget.
  235. |
  236. | With no argument return the list of all bindtags associated with
  237. | this widget. With a list of strings as argument the bindtags are
  238. | set to this list. The bindtags determine in which order events are
  239. | processed (see bind).
  240. |
  241. | cget(self, key)
  242. | Return the resource value for a KEY given as string.
  243. |
  244. | clipboard_append(self, string, **kw)
  245. | Append STRING to the Tk clipboard.
  246. |
  247. | A widget specified at the optional displayof keyword
  248. | argument specifies the target display. The clipboard
  249. | can be retrieved with selection_get.
  250. |
  251. | clipboard_clear(self, **kw)
  252. | Clear the data in the Tk clipboard.
  253. |
  254. | A widget specified for the optional displayof keyword
  255. | argument specifies the target display.
  256. |
  257. | clipboard_get(self, **kw)
  258. | Retrieve data from the clipboard on window's display.
  259. |
  260. | The window keyword defaults to the root window of the Tkinter
  261. | application.
  262. |
  263. | The type keyword specifies the form in which the data is
  264. | to be returned and should be an atom name such as STRING
  265. | or FILE_NAME. Type defaults to STRING, except on X11, where the default
  266. | is to try UTF8_STRING and fall back to STRING.
  267. |
  268. | This command is equivalent to:
  269. |
  270. | selection_get(CLIPBOARD)
  271. |
  272. | columnconfigure = grid_columnconfigure(self, index, cnf={}, **kw)
  273. |
  274. | config = configure(self, cnf=None, **kw)
  275. |
  276. | configure(self, cnf=None, **kw)
  277. | Configure resources of a widget.
  278. |
  279. | The values for resources are specified as keyword
  280. | arguments. To get an overview about
  281. | the allowed keyword arguments call the method keys.
  282. |
  283. | deletecommand(self, name)
  284. | Internal function.
  285. |
  286. | Delete the Tcl command provided in NAME.
  287. |
  288. | event_add(self, virtual, *sequences)
  289. | Bind a virtual event VIRTUAL (of the form <<Name>>)
  290. | to an event SEQUENCE such that the virtual event is triggered
  291. | whenever SEQUENCE occurs.
  292. |
  293. | event_delete(self, virtual, *sequences)
  294. | Unbind a virtual event VIRTUAL from SEQUENCE.
  295. |
  296. | event_generate(self, sequence, **kw)
  297. | Generate an event SEQUENCE. Additional
  298. | keyword arguments specify parameter of the event
  299. | (e.g. x, y, rootx, rooty).
  300. |
  301. | event_info(self, virtual=None)
  302. | Return a list of all virtual events or the information
  303. | about the SEQUENCE bound to the virtual event VIRTUAL.
  304. |
  305. | focus = focus_set(self)
  306. |
  307. | focus_displayof(self)
  308. | Return the widget which has currently the focus on the
  309. | display where this widget is located.
  310. |
  311. | Return None if the application does not have the focus.
  312. |
  313. | focus_force(self)
  314. | Direct input focus to this widget even if the
  315. | application does not have the focus. Use with
  316. | caution!
  317. |
  318. | focus_get(self)
  319. | Return the widget which has currently the focus in the
  320. | application.
  321. |
  322. | Use focus_displayof to allow working with several
  323. | displays. Return None if application does not have
  324. | the focus.
  325. |
  326. | focus_lastfor(self)
  327. | Return the widget which would have the focus if top level
  328. | for this widget gets the focus from the window manager.
  329. |
  330. | focus_set(self)
  331. | Direct input focus to this widget.
  332. |
  333. | If the application currently does not have the focus
  334. | this widget will get the focus if the application gets
  335. | the focus through the window manager.
  336. |
  337. | getboolean(self, s)
  338. | Return a boolean value for Tcl boolean values true and false given as parameter.
  339. |
  340. | getdouble(self, s)
  341. |
  342. | getint(self, s)
  343. |
  344. | getvar(self, name='PY_VAR')
  345. | Return value of Tcl variable NAME.
  346. |
  347. | grab_current(self)
  348. | Return widget which has currently the grab in this application
  349. | or None.
  350. |
  351. | grab_release(self)
  352. | Release grab for this widget if currently set.
  353. |
  354. | grab_set(self)
  355. | Set grab for this widget.
  356. |
  357. | A grab directs all events to this and descendant
  358. | widgets in the application.
  359. |
  360. | grab_set_global(self)
  361. | Set global grab for this widget.
  362. |
  363. | A global grab directs all events to this and
  364. | descendant widgets on the display. Use with caution -
  365. | other applications do not get events anymore.
  366. |
  367. | grab_status(self)
  368. | Return None, "local" or "global" if this widget has
  369. | no, a local or a global grab.
  370. |
  371. | grid_anchor(self, anchor=None)
  372. | The anchor value controls how to place the grid within the
  373. | master when no row/column has any weight.
  374. |
  375. | The default anchor is nw.
  376. |
  377. | grid_bbox(self, column=None, row=None, col2=None, row2=None)
  378. | Return a tuple of integer coordinates for the bounding
  379. | box of this widget controlled by the geometry manager grid.
  380. |
  381. | If COLUMN, ROW is given the bounding box applies from
  382. | the cell with row and column 0 to the specified
  383. | cell. If COL2 and ROW2 are given the bounding box
  384. | starts at that cell.
  385. |
  386. | The returned integers specify the offset of the upper left
  387. | corner in the master widget and the width and height.
  388. |
  389. | grid_columnconfigure(self, index, cnf={}, **kw)
  390. | Configure column INDEX of a grid.
  391. |
  392. | Valid resources are minsize (minimum size of the column),
  393. | weight (how much does additional space propagate to this column)
  394. | and pad (how much space to let additionally).
  395. |
  396. | grid_location(self, x, y)
  397. | Return a tuple of column and row which identify the cell
  398. | at which the pixel at position X and Y inside the master
  399. | widget is located.
  400. |
  401. | grid_propagate(self, flag=['_noarg_'])
  402. | Set or get the status for propagation of geometry information.
  403. |
  404. | A boolean argument specifies whether the geometry information
  405. | of the slaves will determine the size of this widget. If no argument
  406. | is given, the current setting will be returned.
  407. |
  408. | grid_rowconfigure(self, index, cnf={}, **kw)
  409. | Configure row INDEX of a grid.
  410. |
  411. | Valid resources are minsize (minimum size of the row),
  412. | weight (how much does additional space propagate to this row)
  413. | and pad (how much space to let additionally).
  414. |
  415. | grid_size(self)
  416. | Return a tuple of the number of column and rows in the grid.
  417. |
  418. | grid_slaves(self, row=None, column=None)
  419. | Return a list of all slaves of this widget
  420. | in its packing order.
  421. |
  422. | image_names(self)
  423. | Return a list of all existing image names.
  424. |
  425. | image_types(self)
  426. | Return a list of all available image types (e.g. photo bitmap).
  427. |
  428. | info_patchlevel(self)
  429. | Returns the exact version of the Tcl library.
  430. |
  431. | keys(self)
  432. | Return a list of all resource names of this widget.
  433. |
  434. | lift = tkraise(self, aboveThis=None)
  435. |
  436. | lower(self, belowThis=None)
  437. | Lower this widget in the stacking order.
  438. |
  439. | mainloop(self, n=0)
  440. | Call the mainloop of Tk.
  441. |
  442. | nametowidget(self, name)
  443. | Return the Tkinter instance of a widget identified by
  444. | its Tcl name NAME.
  445. |
  446. | option_add(self, pattern, value, priority=None)
  447. | Set a VALUE (second parameter) for an option
  448. | PATTERN (first parameter).
  449. |
  450. | An optional third parameter gives the numeric priority
  451. | (defaults to 80).
  452. |
  453. | option_clear(self)
  454. | Clear the option database.
  455. |
  456. | It will be reloaded if option_add is called.
  457. |
  458. | option_get(self, name, className)
  459. | Return the value for an option NAME for this widget
  460. | with CLASSNAME.
  461. |
  462. | Values with higher priority override lower values.
  463. |
  464. | option_readfile(self, fileName, priority=None)
  465. | Read file FILENAME into the option database.
  466. |
  467. | An optional second parameter gives the numeric
  468. | priority.
  469. |
  470. | pack_propagate(self, flag=['_noarg_'])
  471. | Set or get the status for propagation of geometry information.
  472. |
  473. | A boolean argument specifies whether the geometry information
  474. | of the slaves will determine the size of this widget. If no argument
  475. | is given the current setting will be returned.
  476. |
  477. | pack_slaves(self)
  478. | Return a list of all slaves of this widget
  479. | in its packing order.
  480. |
  481. | place_slaves(self)
  482. | Return a list of all slaves of this widget
  483. | in its packing order.
  484. |
  485. | propagate = pack_propagate(self, flag=['_noarg_'])
  486. |
  487. | quit(self)
  488. | Quit the Tcl interpreter. All widgets will be destroyed.
  489. |
  490. | register = _register(self, func, subst=None, needcleanup=1)
  491. |
  492. | rowconfigure = grid_rowconfigure(self, index, cnf={}, **kw)
  493. |
  494. | selection_get(self, **kw)
  495. | Return the contents of the current X selection.
  496. |
  497. | A keyword parameter selection specifies the name of
  498. | the selection and defaults to PRIMARY. A keyword
  499. | parameter displayof specifies a widget on the display
  500. | to use. A keyword parameter type specifies the form of data to be
  501. | fetched, defaulting to STRING except on X11, where UTF8_STRING is tried
  502. | before STRING.
  503. |
  504. | selection_handle(self, command, **kw)
  505. | Specify a function COMMAND to call if the X
  506. | selection owned by this widget is queried by another
  507. | application.
  508. |
  509. | This function must return the contents of the
  510. | selection. The function will be called with the
  511. | arguments OFFSET and LENGTH which allows the chunking
  512. | of very long selections. The following keyword
  513. | parameters can be provided:
  514. | selection - name of the selection (default PRIMARY),
  515. | type - type of the selection (e.g. STRING, FILE_NAME).
  516. |
  517. | selection_own(self, **kw)
  518. | Become owner of X selection.
  519. |
  520. | A keyword parameter selection specifies the name of
  521. | the selection (default PRIMARY).
  522. |
  523. | selection_own_get(self, **kw)
  524. | Return owner of X selection.
  525. |
  526. | The following keyword parameter can
  527. | be provided:
  528. | selection - name of the selection (default PRIMARY),
  529. | type - type of the selection (e.g. STRING, FILE_NAME).
  530. |
  531. | send(self, interp, cmd, *args)
  532. | Send Tcl command CMD to different interpreter INTERP to be executed.
  533. |
  534. | setvar(self, name='PY_VAR', value='1')
  535. | Set Tcl variable NAME to VALUE.
  536. |
  537. | size = grid_size(self)
  538. |
  539. | slaves = pack_slaves(self)
  540. |
  541. | tk_bisque(self)
  542. | Change the color scheme to light brown as used in Tk 3.6 and before.
  543. |
  544. | tk_focusFollowsMouse(self)
  545. | The widget under mouse will get automatically focus. Can not
  546. | be disabled easily.
  547. |
  548. | tk_focusNext(self)
  549. | Return the next widget in the focus order which follows
  550. | widget which has currently the focus.
  551. |
  552. | The focus order first goes to the next child, then to
  553. | the children of the child recursively and then to the
  554. | next sibling which is higher in the stacking order. A
  555. | widget is omitted if it has the takefocus resource set
  556. | to 0.
  557. |
  558. | tk_focusPrev(self)
  559. | Return previous widget in the focus order. See tk_focusNext for details.
  560. |
  561. | tk_setPalette(self, *args, **kw)
  562. | Set a new color scheme for all widget elements.
  563. |
  564. | A single color as argument will cause that all colors of Tk
  565. | widget elements are derived from this.
  566. | Alternatively several keyword parameters and its associated
  567. | colors can be given. The following keywords are valid:
  568. | activeBackground, foreground, selectColor,
  569. | activeForeground, highlightBackground, selectBackground,
  570. | background, highlightColor, selectForeground,
  571. | disabledForeground, insertBackground, troughColor.
  572. |
  573. | tk_strictMotif(self, boolean=None)
  574. | Set Tcl internal variable, whether the look and feel
  575. | should adhere to Motif.
  576. |
  577. | A parameter of 1 means adhere to Motif (e.g. no color
  578. | change if mouse passes over slider).
  579. | Returns the set value.
  580. |
  581. | tkraise(self, aboveThis=None)
  582. | Raise this widget in the stacking order.
  583. |
  584. | unbind(self, sequence, funcid=None)
  585. | Unbind for this widget for event SEQUENCE the
  586. | function identified with FUNCID.
  587. |
  588. | unbind_all(self, sequence)
  589. | Unbind for all widgets for event SEQUENCE all functions.
  590. |
  591. | unbind_class(self, className, sequence)
  592. | Unbind for all widgets with bindtag CLASSNAME for event SEQUENCE
  593. | all functions.
  594. |
  595. | update(self)
  596. | Enter event loop until all pending events have been processed by Tcl.
  597. |
  598. | update_idletasks(self)
  599. | Enter event loop until all idle callbacks have been called. This
  600. | will update the display of windows but not process events caused by
  601. | the user.
  602. |
  603. | wait_variable(self, name='PY_VAR')
  604. | Wait until the variable is modified.
  605. |
  606. | A parameter of type IntVar, StringVar, DoubleVar or
  607. | BooleanVar must be given.
  608. |
  609. | wait_visibility(self, window=None)
  610. | Wait until the visibility of a WIDGET changes
  611. | (e.g. it appears).
  612. |
  613. | If no parameter is given self is used.
  614. |
  615. | wait_window(self, window=None)
  616. | Wait until a WIDGET is destroyed.
  617. |
  618. | If no parameter is given self is used.
  619. |
  620. | waitvar = wait_variable(self, name='PY_VAR')
  621. |
  622. | winfo_atom(self, name, displayof=0)
  623. | Return integer which represents atom NAME.
  624. |
  625. | winfo_atomname(self, id, displayof=0)
  626. | Return name of atom with identifier ID.
  627. |
  628. | winfo_cells(self)
  629. | Return number of cells in the colormap for this widget.
  630. |
  631. | winfo_children(self)
  632. | Return a list of all widgets which are children of this widget.
  633. |
  634. | winfo_class(self)
  635. | Return window class name of this widget.
  636. |
  637. | winfo_colormapfull(self)
  638. | Return True if at the last color request the colormap was full.
  639. |
  640. | winfo_containing(self, rootX, rootY, displayof=0)
  641. | Return the widget which is at the root coordinates ROOTX, ROOTY.
  642. |
  643. | winfo_depth(self)
  644. | Return the number of bits per pixel.
  645. |
  646. | winfo_exists(self)
  647. | Return true if this widget exists.
  648. |
  649. | winfo_fpixels(self, number)
  650. | Return the number of pixels for the given distance NUMBER
  651. | (e.g. "3c") as float.
  652. |
  653. | winfo_geometry(self)
  654. | Return geometry string for this widget in the form "widthxheight+X+Y".
  655. |
  656. | winfo_height(self)
  657. | Return height of this widget.
  658. |
  659. | winfo_id(self)
  660. | Return identifier ID for this widget.
  661. |
  662. | winfo_interps(self, displayof=0)
  663. | Return the name of all Tcl interpreters for this display.
  664. |
  665. | winfo_ismapped(self)
  666. | Return true if this widget is mapped.
  667. |
  668. | winfo_manager(self)
  669. | Return the window manager name for this widget.
  670. |
  671. | winfo_name(self)
  672. | Return the name of this widget.
  673. |
  674. | winfo_parent(self)
  675. | Return the name of the parent of this widget.
  676. |
  677. | winfo_pathname(self, id, displayof=0)
  678. | Return the pathname of the widget given by ID.
  679. |
  680. | winfo_pixels(self, number)
  681. | Rounded integer value of winfo_fpixels.
  682. |
  683. | winfo_pointerx(self)
  684. | Return the x coordinate of the pointer on the root window.
  685. |
  686. | winfo_pointerxy(self)
  687. | Return a tuple of x and y coordinates of the pointer on the root window.
  688. |
  689. | winfo_pointery(self)
  690. | Return the y coordinate of the pointer on the root window.
  691. |
  692. | winfo_reqheight(self)
  693. | Return requested height of this widget.
  694. |
  695. | winfo_reqwidth(self)
  696. | Return requested width of this widget.
  697. |
  698. | winfo_rgb(self, color)
  699. | Return a tuple of integer RGB values in range(65536) for color in this widget.
  700. |
  701. | winfo_rootx(self)
  702. | Return x coordinate of upper left corner of this widget on the
  703. | root window.
  704. |
  705. | winfo_rooty(self)
  706. | Return y coordinate of upper left corner of this widget on the
  707. | root window.
  708. |
  709. | winfo_screen(self)
  710. | Return the screen name of this widget.
  711. |
  712. | winfo_screencells(self)
  713. | Return the number of the cells in the colormap of the screen
  714. | of this widget.
  715. |
  716. | winfo_screendepth(self)
  717. | Return the number of bits per pixel of the root window of the
  718. | screen of this widget.
  719. |
  720. | winfo_screenheight(self)
  721. | Return the number of pixels of the height of the screen of this widget
  722. | in pixel.
  723. |
  724. | winfo_screenmmheight(self)
  725. | Return the number of pixels of the height of the screen of
  726. | this widget in mm.
  727. |
  728. | winfo_screenmmwidth(self)
  729. | Return the number of pixels of the width of the screen of
  730. | this widget in mm.
  731. |
  732. | winfo_screenvisual(self)
  733. | Return one of the strings directcolor, grayscale, pseudocolor,
  734. | staticcolor, staticgray, or truecolor for the default
  735. | colormodel of this screen.
  736. |
  737. | winfo_screenwidth(self)
  738. | Return the number of pixels of the width of the screen of
  739. | this widget in pixel.
  740. |
  741. | winfo_server(self)
  742. | Return information of the X-Server of the screen of this widget in
  743. | the form "XmajorRminor vendor vendorVersion".
  744. |
  745. | winfo_toplevel(self)
  746. | Return the toplevel widget of this widget.
  747. |
  748. | winfo_viewable(self)
  749. | Return true if the widget and all its higher ancestors are mapped.
  750. |
  751. | winfo_visual(self)
  752. | Return one of the strings directcolor, grayscale, pseudocolor,
  753. | staticcolor, staticgray, or truecolor for the
  754. | colormodel of this widget.
  755. |
  756. | winfo_visualid(self)
  757. | Return the X identifier for the visual for this widget.
  758. |
  759. | winfo_visualsavailable(self, includeids=False)
  760. | Return a list of all visuals available for the screen
  761. | of this widget.
  762. |
  763. | Each item in the list consists of a visual name (see winfo_visual), a
  764. | depth and if includeids is true is given also the X identifier.
  765. |
  766. | winfo_vrootheight(self)
  767. | Return the height of the virtual root window associated with this
  768. | widget in pixels. If there is no virtual root window return the
  769. | height of the screen.
  770. |
  771. | winfo_vrootwidth(self)
  772. | Return the width of the virtual root window associated with this
  773. | widget in pixel. If there is no virtual root window return the
  774. | width of the screen.
  775. |
  776. | winfo_vrootx(self)
  777. | Return the x offset of the virtual root relative to the root
  778. | window of the screen of this widget.
  779. |
  780. | winfo_vrooty(self)
  781. | Return the y offset of the virtual root relative to the root
  782. | window of the screen of this widget.
  783. |
  784. | winfo_width(self)
  785. | Return the width of this widget.
  786. |
  787. | winfo_x(self)
  788. | Return the x coordinate of the upper left corner of this widget
  789. | in the parent.
  790. |
  791. | winfo_y(self)
  792. | Return the y coordinate of the upper left corner of this widget
  793. | in the parent.
  794. |
  795. | ----------------------------------------------------------------------
  796. | Data descriptors inherited from tkinter.Misc:
  797. |
  798. | __dict__
  799. | dictionary for instance variables (if defined)
  800. |
  801. | __weakref__
  802. | list of weak references to the object (if defined)
  803. |
  804. | ----------------------------------------------------------------------
  805. | Methods inherited from tkinter.Pack:
  806. |
  807. | forget = pack_forget(self)
  808. |
  809. | info = pack_info(self)
  810. |
  811. | pack = pack_configure(self, cnf={}, **kw)
  812. |
  813. | pack_configure(self, cnf={}, **kw)
  814. | Pack a widget in the parent widget. Use as options:
  815. | after=widget - pack it after you have packed widget
  816. | anchor=NSEW (or subset) - position widget according to
  817. | given direction
  818. | before=widget - pack it before you will pack widget
  819. | expand=bool - expand widget if parent size grows
  820. | fill=NONE or X or Y or BOTH - fill widget if widget grows
  821. | in=master - use master to contain this widget
  822. | in_=master - see 'in' option description
  823. | ipadx=amount - add internal padding in x direction
  824. | ipady=amount - add internal padding in y direction
  825. | padx=amount - add padding in x direction
  826. | pady=amount - add padding in y direction
  827. | side=TOP or BOTTOM or LEFT or RIGHT - where to add this widget.
  828. |
  829. | pack_forget(self)
  830. | Unmap this widget and do not use it for the packing order.
  831. |
  832. | pack_info(self)
  833. | Return information about the packing options
  834. | for this widget.
  835. |
  836. | ----------------------------------------------------------------------
  837. | Methods inherited from tkinter.Place:
  838. |
  839. | place = place_configure(self, cnf={}, **kw)
  840. |
  841. | place_configure(self, cnf={}, **kw)
  842. | Place a widget in the parent widget. Use as options:
  843. | in=master - master relative to which the widget is placed
  844. | in_=master - see 'in' option description
  845. | x=amount - locate anchor of this widget at position x of master
  846. | y=amount - locate anchor of this widget at position y of master
  847. | relx=amount - locate anchor of this widget between 0.0 and 1.0
  848. | relative to width of master (1.0 is right edge)
  849. | rely=amount - locate anchor of this widget between 0.0 and 1.0
  850. | relative to height of master (1.0 is bottom edge)
  851. | anchor=NSEW (or subset) - position anchor according to given direction
  852. | width=amount - width of this widget in pixel
  853. | height=amount - height of this widget in pixel
  854. | relwidth=amount - width of this widget between 0.0 and 1.0
  855. | relative to width of master (1.0 is the same width
  856. | as the master)
  857. | relheight=amount - height of this widget between 0.0 and 1.0
  858. | relative to height of master (1.0 is the same
  859. | height as the master)
  860. | bordermode="inside" or "outside" - whether to take border width of
  861. | master widget into account
  862. |
  863. | place_forget(self)
  864. | Unmap this widget.
  865. |
  866. | place_info(self)
  867. | Return information about the placing options
  868. | for this widget.
  869. |
  870. | ----------------------------------------------------------------------
  871. | Methods inherited from tkinter.Grid:
  872. |
  873. | grid = grid_configure(self, cnf={}, **kw)
  874. |
  875. | grid_configure(self, cnf={}, **kw)
  876. | Position a widget in the parent widget in a grid. Use as options:
  877. | column=number - use cell identified with given column (starting with 0)
  878. | columnspan=number - this widget will span several columns
  879. | in=master - use master to contain this widget
  880. | in_=master - see 'in' option description
  881. | ipadx=amount - add internal padding in x direction
  882. | ipady=amount - add internal padding in y direction
  883. | padx=amount - add padding in x direction
  884. | pady=amount - add padding in y direction
  885. | row=number - use cell identified with given row (starting with 0)
  886. | rowspan=number - this widget will span several rows
  887. | sticky=NSEW - if cell is larger on which sides will this
  888. | widget stick to the cell boundary
  889. |
  890. | grid_forget(self)
  891. | Unmap this widget.
  892. |
  893. | grid_info(self)
  894. | Return information about the options
  895. | for positioning this widget in a grid.
  896. |
  897. | grid_remove(self)
  898. | Unmap this widget but remember the grid options.
  899. |
  900. | location = grid_location(self, x, y)
  901. |
  902. | ----------------------------------------------------------------------
  903. | Methods inherited from tkinter.XView:
  904. |
  905. | xview(self, *args)
  906. | Query and change the horizontal position of the view.
  907. |
  908. | xview_moveto(self, fraction)
  909. | Adjusts the view in the window so that FRACTION of the
  910. | total width of the canvas is off-screen to the left.
  911. |
  912. | xview_scroll(self, number, what)
  913. | Shift the x-view according to NUMBER which is measured in "units"
  914. | or "pages" (WHAT).

附件二
tkinter.rar下载
tkinter全部控件的英文帮助全集下载地址:

https://download.csdn.net/download/boysoft2002/88640897

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原文链接:Python tkinter控件全集之组合选择框 ttk.ComboBox_ttk combobox-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/813353
推荐阅读
相关标签
  

闽ICP备14008679号