简体   繁体   English

在我的 Tkinter gui 中,我能否让我的选项菜单只显示在一个选项卡上而不是所有三个选项卡上?

[英]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.

相关问题 为什么我的查询仅在我的sqlalchemy flask应用程序中显示一个变量,而不是所有变量? - Why is my query only showing one variable in my sqlalchemy flask app rather than all? TkInter:如何使对象出现在第二个而不是第一个窗口上? - TkInter: how can I make objects appear on my second window rather than the first? 我的 function 只显示我数据库中的一个元组,我怎样才能让它显示所有这些? - My function is only displaying one tuple from my database , how can I make it display all of them? 我无法将我的pyame游戏链接到我的Tkinter GUI - I can't link my pyame game to my tkinter GUI 为什么菜单没有显示在我的 Tkinter GUI 上? - Why is the menu not showing on my Tkinter GUI? 我无法在Tkinter屏幕上显示答案 - I can not display the answer on my tkinter screen 更改一个菜单的值时,为什么所有我的tkinter选项菜单都会更新? - Why are all my tkinter optionmenus being updated when I change the values of one menu? 如何强制菜单位于 window 而不是 OSX 默认值 Tkinter - How can I force the Menu to be in the window rather than the OSX default Tkinter Tkinter 一次显示所有页面,而不是一页一页地显示 - Tkinter shows all pages at once rather than one by one 我可以在本地目录而不是〜/ local中安装pip吗? - Can I install pip in my local directory rather than ~/local?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM