下拉菜单未显示在Combobox Tkinter中
我正在尝试为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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,我不得不修复一些内容才能运行此操作(即,Instantiating
app = cpumon()
和调用app.mainloop()
。同样,我更换了<< 共享exit_app()
的代码代码> self.exit_app 带有
self.quit
,因为您没有 否则,我会看到combo_win
下拉列出的值是默认情况下的
combo_win
是空的(默认情况下,使用给出的代码),您需要使用以下方式设置初始选择:So, I had to fix a few things to get this to run (i.e., instantiating
app = cpuMon()
and callingapp.mainloop()
. Likewise, I replacedself.exit_app
withself.quit
since you didn't share the code forexit_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: