在Tkinter窗口中,现有框架下方的元素。

huangapple go评论91阅读模式
英文:

Element in Tkinter window below existing Frame

问题

我想使用Tkinter在Python中创建一个程序。它会生成多种类型的输出,每种输出需要不同的输入。

为了区分不同的输入,我使用多个选项卡。

然后,有一些输入对所有输出都是相同的;是否可以将它们的输入放在窗口上,与选项卡无关?

窗口应该看起来有点像这样,单选按钮始终可见:
在Tkinter窗口中,现有框架下方的元素。

Python程序如下:

  1. from tkinter import *
  2. window = Tk()
  3. tabControl = ttk.Notebook(window)
  4. tabMakeTea = ttk.Frame(tabControl); tabControl.add(tabMakeTea, text="制茶")
  5. tabMakeCoffee = ttk.Frame(tabControl); tabControl.add(tabMakeCoffee, text="制咖啡")
  6. tabControl.pack(expand=1, fill="both")
  7. window.mainloop()

现在我将继续在每个选项卡中添加元素;接下来,我需要一些东西,无论你在“制茶”或“制咖啡”选项卡中都应该可见。

首先,我将相同的输入放在多个框架中,希望数据将从相应(活动的)框架中读取,但Python从一个(始终相同的)输入中读取值。

接下来,我尝试将元素放在window元素上,以便无论框架如何都可见,但这似乎不起作用:

  1. Label(window, text="糖: ").grid(row=0, column=0)

目前,我为这种类型的输入有另一个Frame,但我发现这不太用户友好。

请问有人可以建议如何实现这个需求吗?

我尝试将元素直接放在“window”元素上,结果出现错误“无法在已经由pack管理的.内使用网格几何管理器”。

我尝试过在谷歌上搜索“将元素添加到多个框架”和“将元素放在Frame元素下的窗口上”都没有成功。

英文:

I want to create a program in Python using Tkinter. It creates several types of outputs, which need different inputs each..

To separate different inputs, i use multiple tabs.

Then, there are inputs which are same for all outputs; is it possible to put their Inputs onto the window, unrelated to the tabs?

The window should look a bit like this with the Radio Buttons always visible:
在Tkinter窗口中,现有框架下方的元素。

The python program is:

  1. from tkinter import *
  2. window = Tk()
  3. tabControl = ttk.Notebook(window)
  4. tabMakeTea = ttk.Frame(tabControl); tabControl.add(tabMakeTea, text = "Make tea")
  5. tabMakeCoffee = ttk.Frame(tabControl); tabControl.add(tabMakeCoffee, text = "Make coffee")
  6. tabControl.pack(expand = 1, fill="both")
  7. window.mainloop()

Now i continue with elements within each Frame; next i need things that should be visible no matter if you are in "tea" or "coffee" tab.

First i placed same inputs to multiple Frames, hoping the data will be read from the respective (active) Frame, but Python reads values from one (always the same) inputs.

Next, i tried to place elements to the window element so that it is always visible regardless of the Frame, but this doesn't seem to work:

  1. Label(window, text = "Sugar: ").grid(row=0, column=0)

Right now i have another Frame for this kind of inputs, but i find it not very user-friendly.

Could you anybody please advise how to get to the

i tried placing the element straight to the "window" element, which results in error "cannot use geometry manager grid inside . which already has slaves managed by pack".

i tried googling "adding element to multiple frames" and "placing element on window bellow Frame element" with no success

答案1

得分: 1

如果你想要将小部件放在选项卡控件之外,只需将它们设为根窗口的子级,而不是选项卡控件。

  1. from tkinter import *
  2. window = Tk()
  3. tabControl = ttk.Notebook(window)
  4. tabMakeTea = ttk.Frame(tabControl) # this widget is a child of tabControl
  5. tabControl.add(tabMakeTea, text="制茶")
  6. tabMakeCoffee = ttk.Frame(tabControl) # this widget is also a child of tabControl
  7. tabControl.add(tabMakeCoffee, text="制咖啡")
  8. tabControl.pack(expand=True, fill="both")
  9. new_frame = Frame(window) # this Frame is a child of the root window
  10. new_frame.pack(expand=True, fill='both')
  11. # this example button is a child of 'new_frame' and should appear below 'tabControl'
  12. new_button = ttk.Button(new_frame, text='你好')
  13. new_button.pack(expand=False, fill='none')
  14. window.mainloop()
英文:

If you want widgets outside of your tab control, you only need to make them children of the root window instead of the tab control

  1. from tkinter import *
  2. window = Tk()
  3. tabControl = ttk.Notebook(window)
  4. tabMakeTea = ttk.Frame(tabControl) # this widget is a child of tabControl
  5. tabControl.add(tabMakeTea, text = "Make tea")
  6. tabMakeCoffee = ttk.Frame(tabControl) # this widget is a also child of tabControl
  7. tabControl.add(tabMakeCoffee, text = "Make coffee")
  8. tabControl.pack(expand=True, fill="both")
  9. new_frame = Frame(window) # this Frame is a child of the root window
  10. new_frame.pack(expand=True, fill='both')
  11. # this example button is a child of 'new_frame', and should appear below 'tabControl'
  12. new_button = ttk.Button(new_frame, text='Hello')
  13. new_button.pack(expand=False, fill='none')
  14. window.mainloop()
  15. </details>

huangapple
  • 本文由 发表于 2023年4月6日 20:30:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949542.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定