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

问题描述 投票:0回答:1

我想使用 Tkinter 在 Python 中创建一个程序。它创建了几种类型的输出,每种输出都需要不同的输入..

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

然后,所有输出都有相同的输入;是否可以将他们的输入放到窗口中,与选项卡无关?

窗口应该看起来有点像这样,单选按钮总是可见的:

python程序为:

from tkinter import *

window = Tk()

tabControl = ttk.Notebook(window)
tabMakeTea = ttk.Frame(tabControl); tabControl.add(tabMakeTea, text = "Make tea")
tabMakeCoffee = ttk.Frame(tabControl); tabControl.add(tabMakeCoffee, text = "Make coffee")

tabControl.pack(expand = 1, fill="both")

window.mainloop()

现在我继续每个框架中的元素;接下来,无论您是在“茶”还是“咖啡”选项卡中,我都需要一些应该可见的东西。

首先我将相同的输入放置到多个框架,希望数据将从各自的(活动的)框架中读取,但是 Python 从一个(总是相同的)输入中读取值。

接下来,我尝试将元素放置到

window
元素中,以便它始终可见,而不管框架如何,但这似乎不起作用:

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

现在我有另一个

Frame
用于这种输入,但我发现它不是很用户友好。

有人能告诉我怎么去吗

我尝试将元素直接放置到“窗口”元素,这导致错误“无法在内部使用几何管理器网格。它已经有由包管理的奴隶”。

我尝试谷歌搜索“向多个框架添加元素”和“将元素放置在框架元素下方的窗口中”但没有成功

python tkinter frame
1个回答
0
投票

如果你想要小部件 outside 你的选项卡控件,你只需要让它们成为根窗口的孩子而不是选项卡控件

from tkinter import *

window = Tk()

tabControl = ttk.Notebook(window)
tabMakeTea = ttk.Frame(tabControl)  # this widget is a child of tabControl
tabControl.add(tabMakeTea, text = "Make tea")
tabMakeCoffee = ttk.Frame(tabControl)  # this widget is a also child of tabControl
tabControl.add(tabMakeCoffee, text = "Make coffee")
tabControl.pack(expand=True, fill="both")

new_frame = Frame(window)  # this Frame is a child of the root window
new_frame.pack(expand=True, fill='both'
# this example button is a child of 'new_frame', and should appear below 'tabControl'
new_button = ttk.Button(new_frame, text='Hello')
new_button.pack(expand=False, fill='none')

window.mainloop()
© www.soinside.com 2019 - 2024. All rights reserved.