[英]Can I have my options menu display only on one tab rather than all three in my Tkinter gui?
I am programming a GUI with three tabs - each tab needs to have its own dropdown menu to select a financial calculation to compute.我正在编写一个具有三个选项卡的 GUI - 每个选项卡都需要有自己的下拉菜单来计算 select 财务计算。 I have coded an options menu but it appears in all three tabs rather than one.
我编写了一个选项菜单,但它出现在所有三个选项卡中,而不是一个。 How can I make an options menu exclusive to one tab?
如何使选项菜单专用于一个选项卡? After trying to put the options menu in a frame my code looked like this:
在尝试将选项菜单放入框架后,我的代码如下所示:
import tkinter as tk
from tkinter import *
from tkinter import ttk
root = tk.Tk()
root.title("Tab Widget")
tabControl = ttk.Notebook(root)
variable = StringVar(root)
variable.set(OPTIONS[0])
w = OptionMenu(root, variable, *OPTIONS)
w.pack()
tab1 = ttk.Frame(tabControl, OPTIONS=["Gross Margin", "Operating Margin", "Net Profit Margin"])
tab2 = ttk.Frame(tabControl)
tab3 = ttk.Frame(tabControl)
tabControl.add(tab1, text='Tab 1')
tabControl.add(tab2, text='Tab 2')
tabControl.add(tab3, text='Tab 3')
tabControl.pack(expand=1, fill="both")
ttk.Label(tab1,
text="Welcome to GeeksForGeeks").grid(column=0, row=0, padx=30, pady=30)
ttk.Label(tab2,
text="Lets dive into the world of computers").grid(column=0, row=0, padx=30, pady=30)
ttk.Label(tab3,
text="My third tab for my coursework").grid(column=0, row=0, padx=30, pady=30)
root.mainloop()
but I still get the error:但我仍然收到错误:
Traceback (most recent call last):
File "/Users/fariz/PycharmProjects/Practice/venv/bin/CW1 B + Fin Comp..py", line 18, in <module>
tab1 = ttk.Frame(tabControl, OPTIONS=["Gross Margin", "Operating Margin", "Net Profit Margin"])
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/ttk.py", line 742, in __init__
Widget.__init__(self, master, "ttk::frame", kw)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/ttk.py", line 559, in __init__
tkinter.Widget.__init__(self, master, widgetname, kw=kw)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tkinter/__init__.py", line 2296, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: unknown option "-OPTIONS"
The following code moves the option menu in to tab 1.以下代码将选项菜单移动到选项卡 1。
import tkinter as tk
from tkinter import ttk
root = tk.Tk()
root.title("Tab Widget")
tabControl = ttk.Notebook(root)
tab1 = ttk.Frame(tabControl)
tab2 = ttk.Frame(tabControl)
tab3 = ttk.Frame(tabControl)
tabControl.add(tab1, text='Tab 1')
tabControl.add(tab2, text='Tab 2')
tabControl.add(tab3, text='Tab 3')
tabControl.pack(expand=1, fill="both")
OPTIONS=["Gross Margin", "Operating Margin", "Net Profit Margin"]
variable = tk.StringVar(root)
variable.set(OPTIONS[0])
w = tk.OptionMenu(tab1, variable, *OPTIONS)
w.grid(column=0,row=1)
ttk.Label(tab1,
text="Welcome to GeeksForGeeks").grid(column=0, row=0, padx=30, pady=30)
ttk.Label(tab2,
text="Lets dive into the world of computers").grid(column=0, row=0, padx=30, pady=30)
ttk.Label(tab3,
text="My third tab for my coursework").grid(column=0, row=0, padx=30, pady=30)
root.mainloop()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.