下拉菜单未显示在Combobox Tkinter中

发布于 2025-01-28 11:02:45 字数 959 浏览 3 评论 0原文

我正在尝试为Tkinter GUI(ComboBox)制作下拉菜单。该代码没有错误,但下拉菜单不起作用。我正在使用pycharm,macos。请参阅下面的代码。

import tkinter as tk
from tkinter import ttk
import sys


class cpuMon(tk.Tk):

    def __init__(self):
        tk.Tk.__init__(self)
        self.geometry('400x400+1+1')
        self.attributes('-alpha', 1)
        self.attributes('-topmost', True)
        self.resizable(False, False)
        self.title('CPU Monitor')

        self.set_ui()

    def set_ui(self):
        exitButton = ttk.Button(self, text='Exit', command=self.exit_app)
        exitButton.pack(fill=tk.X)

        self.bar2 = ttk.LabelFrame(self, text='Manual')
        self.bar2.pack(fill=tk.X)

        ttk.Button(self.bar2, text='Move').pack(side=tk.LEFT)
        ttk.Button(self.bar2, text='>>>').pack(side=tk.LEFT)

        self.combo_win = ttk.Combobox(self.bar2, state='readonly', values=["hide", "don't hide", "min"])
        self.combo_win.pack(side=tk.LEFT)

I'm trying to make a drop-down menu for tkinter gui (Combobox). The code has no errors, but drop-down menu is not working. I'm using PyCharm, macOS. Please see the code below.

import tkinter as tk
from tkinter import ttk
import sys


class cpuMon(tk.Tk):

    def __init__(self):
        tk.Tk.__init__(self)
        self.geometry('400x400+1+1')
        self.attributes('-alpha', 1)
        self.attributes('-topmost', True)
        self.resizable(False, False)
        self.title('CPU Monitor')

        self.set_ui()

    def set_ui(self):
        exitButton = ttk.Button(self, text='Exit', command=self.exit_app)
        exitButton.pack(fill=tk.X)

        self.bar2 = ttk.LabelFrame(self, text='Manual')
        self.bar2.pack(fill=tk.X)

        ttk.Button(self.bar2, text='Move').pack(side=tk.LEFT)
        ttk.Button(self.bar2, text='>>>').pack(side=tk.LEFT)

        self.combo_win = ttk.Combobox(self.bar2, state='readonly', values=["hide", "don't hide", "min"])
        self.combo_win.pack(side=tk.LEFT)

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

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

发布评论

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

评论(1

烂人 2025-02-04 11:02:45

因此,我不得不修复一些内容才能运行此操作(即,Instantiating app = cpumon()和调用app.mainloop()。同样,我更换了<< 共享exit_app()的代码

代码> self.exit_app 带有self.quit,因为您没有 否则,我会看到combo_win下拉列出的值是默认

情况下的combo_win是空的(默认情况下,使用给出的代码),您需要使用以下方式设置初始选择:

self.combo_win.current(0)  # set the combo box to 'hide' by default

So, I had to fix a few things to get this to run (i.e., instantiating app = cpuMon() and calling app.mainloop(). Likewise, I replaced self.exit_app with self.quit since you didn't share the code for exit_app().

But without modifying the code otherwise, I see that the combo_win dropdown is populated with the values given.

If the issue you're having is that combo_win is empty by default (which it is, using the code given), you'll need to set the initial selection with:

self.combo_win.current(0)  # set the combo box to 'hide' by default
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文