在表格列或小部件中添加一个依赖下拉菜单,可以通过以下步骤来实现:
from tkinter import *
root = Tk()
root.title("Dependency Dropdown")
root.geometry("300x200")
# 设置选项列表
options = ["Option 1", "Option 2", "Option 3"]
selected_option = StringVar(root)
selected_option.set(options[0])
# 创建下拉菜单
dropdown_menu = OptionMenu(root, selected_option, *options)
dropdown_menu.pack()
root.mainloop()
from tkinter import ttk
import tkinter as tk
from tkinter import *
root = Tk()
root.title("Dependency Dropdown")
root.geometry("400x300")
# 创建表格
table = ttk.Treeview(root)
table.pack()
# 添加表格列
table["columns"] = ("Column 1", "Column 2")
# 设置表格列宽度
table.column("#0", width=100)
table.column("Column 1", width=100)
table.column("Column 2", width=100)
# 设置表格列标题
table.heading("#0", text="ID")
table.heading("Column 1", text="Name")
table.heading("Column 2", text="Dependency")
# 添加表格行和数据
table.insert("", "end", text="1", values=("Item 1", "Dependency 1"))
table.insert("", "end", text="2", values=("Item 2", "Dependency 2"))
# 创建下拉菜单
options = ["Dependency 1", "Dependency 2", "Dependency 3"]
selected_option = StringVar(root)
selected_option.set(options[0])
dropdown_menu = OptionMenu(root, selected_option, *options)
# 添加下拉菜单到表格列
table.set("1", "Column 2", dropdown_menu)
table.set("2", "Column 2", dropdown_menu)
root.mainloop()
上述代码创建了一个包含两列的表格,第二列是一个依赖下拉菜单。表格中的每一行都有一个下拉菜单,用户可以在下拉菜单中选择依赖项。
上一篇:表格里的文本
系统代码
下一篇:表格列被识别为“总结”