使用 tkinter 的条目选择列表

发布于 2024-12-06 03:02:42 字数 879 浏览 3 评论 0原文

如何使用 Python tkinter 生成正常的选择列表(如任何 HTML 表单中的邮政地址的“州”字段)?如下图所示,列表框小部件始终在一个大框中显示所有选择,如果将高度减小到 1,则选择时不会展开列表。 OptionMenu 小部件在选择时会正确弹出列表,但在关闭时不会在类似条目的框中显示当前值。 Entry 小部件具有所需的外观,但没有关联的值列表。

请不要告诉我 tkinter 无法进行基本的表单选择:-(。

from tkinter import *

class App:
    def __init__(self, master):

        frame = Frame(master)
        frame.pack()

        items = ["Apple", "Banana", "Cherry"]
        self.list = Listbox(frame, width=8, height=1)
        for item in items:
            self.list.insert(END, item)
        self.list.pack(side=LEFT)

        fruit = StringVar()
        fruit.set(items[1])
        self.menu = OptionMenu(frame, fruit, *items)
        self.menu.pack(side=LEFT)

        self.entry = Entry(frame, width=8)
        self.entry.insert(0, items[2])
        self.entry.pack(side=LEFT)

root = Tk()
app = App(root)
root.mainloop()

How can I generate a normal selection list (like the "State" field in any HTML form for postal addresses) using Python tkinter? As illustrated below, the Listbox widget displays all the selections in a large box all the time, and if you reduce the height to 1, it does not expand the list when selected. The OptionMenu widget pops up the list correctly when selected, but doesn't display the current value in an Entry-like box when closed. And the Entry widget has the desired appearance but doesn't have an associated list of values.

Please don't tell me that tkinter can't do a basic form selection :-(.

from tkinter import *

class App:
    def __init__(self, master):

        frame = Frame(master)
        frame.pack()

        items = ["Apple", "Banana", "Cherry"]
        self.list = Listbox(frame, width=8, height=1)
        for item in items:
            self.list.insert(END, item)
        self.list.pack(side=LEFT)

        fruit = StringVar()
        fruit.set(items[1])
        self.menu = OptionMenu(frame, fruit, *items)
        self.menu.pack(side=LEFT)

        self.entry = Entry(frame, width=8)
        self.entry.insert(0, items[2])
        self.entry.pack(side=LEFT)

root = Tk()
app = App(root)
root.mainloop()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

唐婉 2024-12-13 03:02:42

您正在寻找组合框小部件,TTK 提供了此小部件:

http:// docs.python.org/dev/library/tkinter.ttk.html

http://www.tkdocs.com/widgets/combobox.html

You're looking for a combo box widget, TTK provides this widget:

http://docs.python.org/dev/library/tkinter.ttk.html

http://www.tkdocs.com/widgets/combobox.html

勿忘初心 2024-12-13 03:02:42

您所要求的称为组合框。如果您使用旧版本的 python (<2.7),您可以使用 tix .ComboBox。对于 python 2.7 及更高版本,您可以使用 ttk.combobox (链接指向最新的 python 3.x 文档,但它与 python 2.7 中的小部件相同)。

What you are asking for is called a combobox. If you are using an old version of python (<2.7) you can use tix.ComboBox. For python 2.7 and beyond you can use ttk.combobox (link points to latest python 3.x documentation, but it is the same widget as in python 2.7).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文