You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python Graphics库实现销售数据搜索GUI的下拉菜单?

问题根源

你代码里的核心问题是混用了Tkinter组件(OptionMenu、StringVar)和Zelle Graphics库的GraphWin,这两个UI框架完全不兼容,导致下拉菜单根本无法在GraphWin窗口里正常渲染显示。

下面给你两个可行的实现方案,选一个贴合你技术栈的就行:


方案1:纯Zelle Graphics库实现下拉选择

如果你想继续用GraphWin这套组件,就得手动模拟下拉菜单的交互逻辑,代码如下:

from graphics import GraphWin, Text, Point, Rectangle, Entry

def search_sales_gui():
    search_sale_menu = GraphWin("Search Sales", 400, 300)
    search_sale_menu.setBackground("white")

    # 标题
    title_text = Text(Point(200, 30), "Search Sales")
    title_text.setSize(18)
    title_text.draw(search_sale_menu)

    # 搜索条件标签
    criteria_label = Text(Point(80, 70), "Search Criteria:")
    criteria_label.draw(search_sale_menu)

    # 下拉菜单核心逻辑
    search_criteria = ["Purchase ID", "Item Sold", "Customer Details", "Country"]
    selected_criteria = search_criteria[0]

    # 下拉显示框(模拟按钮)
    dropdown_box = Rectangle(Point(160, 55), Point(300, 85))
    dropdown_box.setFill("white")
    dropdown_box.draw(search_sale_menu)
    dropdown_text = Text(Point(230, 70), selected_criteria)
    dropdown_text.draw(search_sale_menu)

    # 下拉箭头
    arrow_text = Text(Point(280, 70), "▼")
    arrow_text.draw(search_sale_menu)

    # 预创建选项列表(初始隐藏)
    options = []
    option_rects = []
    for i, criteria in enumerate(search_criteria):
        rect = Rectangle(Point(160, 85 + i*30), Point(300, 115 + i*30))
        rect.setFill("#f0f0f0")
        text = Text(Point(230, 100 + i*30), criteria)
        option_rects.append(rect)
        options.append((rect, text))

    dropdown_open = False

    # 关键词输入框
    keyword_label = Text(Point(80, 130), "Keyword:")
    keyword_label.draw(search_sale_menu)
    keyword_entry = Entry(Point(230, 130), 20)
    keyword_entry.draw(search_sale_menu)

    # 搜索按钮
    search_button = Rectangle(Point(160, 180), Point(240, 210))
    search_button.setFill("lightblue")
    search_button.draw(search_sale_menu)
    search_text = Text(Point(200, 195), "Search")
    search_text.draw(search_sale_menu)

    while True:
        click_point = search_sale_menu.getMouse()
        x, y = click_point.getX(), click_point.getY()

        # 点击下拉触发器,切换选项显示/隐藏
        if 160 < x < 300 and 55 < y < 85:
            dropdown_open = not dropdown_open
            if dropdown_open:
                for rect, text in options:
                    rect.draw(search_sale_menu)
                    text.draw(search_sale_menu)
            else:
                for rect, text in options:
                    rect.undraw()
                    text.undraw()
        # 点击选项,更新选中值并关闭下拉
        elif dropdown_open:
            for i, (rect, text) in enumerate(options):
                p1, p2 = rect.getP1(), rect.getP2()
                if p1.getX() < x < p2.getX() and p1.getY() < y < p2.getY():
                    selected_criteria = search_criteria[i]
                    dropdown_text.setText(selected_criteria)
                    dropdown_open = False
                    for r, t in options:
                        r.undraw()
                        t.undraw()
                    break
        # 点击搜索按钮,执行搜索逻辑
        elif 160 < x < 240 and 180 < y < 210:
            keyword = keyword_entry.getText()
            # 替换成你的实际搜索逻辑
            print(f"执行搜索:条件[{selected_criteria}],关键词[{keyword}]")
            search_sale_menu.close()
            break

# 调用测试
search_sales_gui()

方案2:纯Tkinter实现(更简洁可靠)

Tkinter是Python自带的GUI库,原生支持下拉选择、输入框等组件,不需要手动处理点击区域,代码更简洁易维护:

import tkinter as tk
from tkinter import ttk

def search_sales_gui():
    def on_search():
        # 获取选中条件和关键词
        selected_criteria = criteria_var.get()
        keyword = keyword_entry.get()
        # 替换成你的实际搜索逻辑
        print(f"执行搜索:条件[{selected_criteria}],关键词[{keyword}]")
        root.destroy()

    # 创建主窗口
    root = tk.Tk()
    root.title("Search Sales")
    root.geometry("400x300")

    # 标题
    title_label = ttk.Label(root, text="Search Sales", font=("Arial", 18))
    title_label.pack(pady=20)

    # 搜索条件行
    criteria_frame = ttk.Frame(root)
    criteria_frame.pack(pady=10, padx=20, fill="x")

    criteria_label = ttk.Label(criteria_frame, text="Search Criteria:")
    criteria_label.pack(side="left")

    search_criteria = ["Purchase ID", "Item Sold", "Customer Details", "Country"]
    criteria_var = tk.StringVar(value=search_criteria[0])
    # 用Combobox实现下拉选择(比旧OptionMenu美观)
    criteria_dropdown = ttk.Combobox(criteria_frame, textvariable=criteria_var, values=search_criteria, state="readonly")
    criteria_dropdown.pack(side="left", padx=10)

    # 关键词输入行
    keyword_frame = ttk.Frame(root)
    keyword_frame.pack(pady=10, padx=20, fill="x")

    keyword_label = ttk.Label(keyword_frame, text="Keyword:")
    keyword_label.pack(side="left")

    keyword_entry = ttk.Entry(keyword_frame, width=20)
    keyword_entry.pack(side="left", padx=10)

    # 搜索按钮
    search_button = ttk.Button(root, text="Search", command=on_search)
    search_button.pack(pady=20)

    root.mainloop()

# 调用测试
search_sales_gui()

内容的提问来源于stack exchange,提问作者João Paulo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 02:42:48