📜  tkinter 中的组合框小部件 | Python

📅  最后修改于: 2022-05-13 01:54:35.803000             🧑  作者: Mango

tkinter 中的组合框小部件 | Python

Python提供了多种 GUI(图形用户界面)类型,例如 PyQT、Tkinter、Kivy、WxPython 和 PySide。其中, tkinter是Python中最常用的 GUI 模块,因为它简单易懂。 Tkinter 这个词来自于 Tk 接口。 tkinter 模块在Python标准库中可用,在Python中编写程序以生成 GUI 时必须导入该模块。

注意: Tkinter(大写 T) 与 tkinter 不同。 Tkinter 在 Python2.x 中使用,在Python.3x 中改为 tkinter。

组合框是列表框和输入字段的组合。它是 Tkinter 小部件之一,其中包含一个向下箭头,可从选项列表中进行选择。它帮助用户根据显示的选项列表进行选择。当用户单击输入字段上的下拉箭头时,会在输入字段下方显示滚动的列表框的弹出窗口。仅当从列表框中选择一个选项时,选定的选项才会显示在输入字段中。

句法:

combobox = ttk.Combobox(master, option=value, ...)

示例 1:未设置默认值的组合框小部件。

# python program demonstrating
# Combobox widget using tkinter
  
  
import tkinter as tk
from tkinter import ttk
  
# Creating tkinter window
window = tk.Tk()
window.title('Combobox')
window.geometry('500x250')
  
# label text for title
ttk.Label(window, text = "GFG Combobox Widget", 
          background = 'green', foreground ="white", 
          font = ("Times New Roman", 15)).grid(row = 0, column = 1)
  
# label
ttk.Label(window, text = "Select the Month :",
          font = ("Times New Roman", 10)).grid(column = 0,
          row = 5, padx = 10, pady = 25)
  
# Combobox creation
n = tk.StringVar()
monthchoosen = ttk.Combobox(window, width = 27, textvariable = n)
  
# Adding combobox drop down list
monthchoosen['values'] = (' January', 
                          ' February',
                          ' March',
                          ' April',
                          ' May',
                          ' June',
                          ' July',
                          ' August',
                          ' September',
                          ' October',
                          ' November',
                          ' December')
  
monthchoosen.grid(column = 1, row = 5)
monthchoosen.current()
window.mainloop()

输出:

示例 2:具有初始默认值的组合框。
我们还可以在 Combobox 小部件中设置初始默认值,如下面的示例代码所示。

import tkinter as tk
from tkinter import ttk
  
# Creating tkinter window
window = tk.Tk()
window.geometry('350x250')
# Label
ttk.Label(window, text = "Select the Month :", 
        font = ("Times New Roman", 10)).grid(column = 0, 
        row = 15, padx = 10, pady = 25)
  
n = tk.StringVar()
monthchoosen = ttk.Combobox(window, width = 27, 
                            textvariable = n)
  
# Adding combobox drop down list
monthchoosen['values'] = (' January', 
                          ' February',
                          ' March',
                          ' April',
                          ' May',
                          ' June', 
                          ' July', 
                          ' August', 
                          ' September', 
                          ' October', 
                          ' November', 
                          ' December')
  
monthchoosen.grid(column = 1, row = 15)
  
# Shows february as a default value
monthchoosen.current(1) 
window.mainloop()

输出: