[英]Problem having ttk.Notebook() tabs centered
I'm trying to create a simple GUI in order to solve structures. 我正在尝试创建一个简单的GUI以解决结构。
The aim is to let the user add tabs to the GUI if he/she wants to start another project even if a tab is already opened. 目的是让用户即使已经打开选项卡也要启动另一个项目,就可以将选项卡添加到GUI。
My main problem right now is that when a tab is created according the following code, it is centered instead of being created from left to right: 现在我的主要问题是,根据以下代码创建选项卡时,该选项卡处于居中状态,而不是从左至右创建:
import tkinter as tk
from tkinter import ttk
class Pytures(ttk.Frame):
def __init__(self, main_window):
super().__init__(main_window)
#Title
main_window.title("Pytures 0.1.1")
#Geometry initialation
width = main_window.winfo_screenwidth()
height = main_window.winfo_screenheight()
scSize = str(width) + 'x' + str(height)
main_window.geometry(scSize)
#Calls the MenuBar function during initialation
self.MenuBar(main_window)
#Initialize first blank tab -> Tab1
self.notebook = ttk.Notebook(self)
self.tab_names = {}
self.tabAttr_names = {}
self.TabClassCreator()
self.TabAttributeCreator()
self.pack()
def TabClassCreator(self):
# It creates new tab classes
global how_many_tabs
how_many_tabs = len(self.tab_names)
if how_many_tabs >= 1:
self.tab_names[how_many_tabs] = 'Tab'+str(how_many_tabs+1)
exec('global {0}\nclass {0}(ttk.Frame):\n\tdef __init__(self, *args, **kwargs):\n\t\tsuper().__init__(*args, **kwargs)'.format(self.tab_names[how_many_tabs]))
else:
self.tab_names[0] = 'Tab1'
exec('global {0}\nclass {0}(ttk.Frame):\n\tdef __init__(self, *args, **kwargs):\n\t\tsuper().__init__(*args, **kwargs)'.format(self.tab_names[0]))
print('Is the class TabN created?')
print('Class TabN created-> {}'.format(eval(self.tab_names[how_many_tabs])))
def TabAttributeCreator(self):
# It creates new tab attributes
if how_many_tabs >= 1:
self.tabAttr_names[how_many_tabs] = 'Tab_'+str(how_many_tabs+1)
exec('self.{0} = {1}(self.notebook)'.format(self.tabAttr_names[how_many_tabs], self.tab_names[how_many_tabs]))
self.notebook.add(eval('self.{0}'.format(self.tabAttr_names[how_many_tabs])), text=self.tab_names[how_many_tabs])
self.notebook.pack(expand=1, fill="both")
else:
self.tabAttr_names[0] = 'Tab_1'
exec('self.{0} = {1}(self.notebook)'.format(self.tabAttr_names[0], self.tab_names[0]))
self.notebook.add(eval('self.{0}'.format(self.tabAttr_names[0])), text=self.tab_names[0])
self.notebook.pack(expand=1, fill="both")
print('Attribute Tab_N created-> '+str(eval('self.Tab_1')))
return eval('self.{}'.format(self.tabAttr_names[how_many_tabs]))
def MenuBar(self, main_window):
global menubar
menubar = tk.Menu(main_window)
filemenu = tk.Menu(menubar, tearoff=0)
filemenu.add_command(label="New (ctrl + n)", command=None)#newTab) #command=create_tab.createTab(tabControl)
filemenu.add_command(label="Open (ctrl + o)", command=None)
filemenu.add_command(label="Save (ctrl + s)", command=None)
filemenu.add_command(label="Save as...", command=None) #saveAs
filemenu.add_command(label="Close (ctrl + k)", command=None)#closeTab)
filemenu.add_separator()
filemenu.add_command(label="Exit", command=self.quit)
menubar.add_cascade(label="File", menu=filemenu)
editmenu = tk.Menu(menubar, tearoff=0)
editmenu.add_command(label="Undo", command=None)
editmenu.add_separator()
editmenu.add_command(label="Cut", command=None)
editmenu.add_command(label="Copy", command=None)
editmenu.add_command(label="Paste", command=None)
editmenu.add_command(label="Delete", command=None)
editmenu.add_command(label="Select All", command=None)
menubar.add_cascade(label="Edit", menu=editmenu)
helpmenu = tk.Menu(menubar, tearoff=0)
helpmenu.add_command(label="Help Index", command=None)
helpmenu.add_command(label="About...", command=None)
menubar.add_cascade(label="Help", menu=helpmenu)
main_window.config(menu=menubar)
if __name__=="__main__":
global main_window, app
main_window = tk.Tk()
app = Pytures(main_window)
app.mainloop()
I wrote it based in this code: 我是根据以下代码编写的:
import tkinter as tk
from tkinter import ttk
class Application(ttk.Frame):
def __init__(self, main_window):
super().__init__(main_window)
main_window.title("Panel de pestañas en Tcl/Tk")
self.notebook = ttk.Notebook(self)
self.greeting_frame = GreetingFrame(self.notebook)
self.notebook.add(self.greeting_frame, text="Saludos", padding=10)
self.about_frame = AboutFrame(self.notebook)
self.notebook.add(self.about_frame, text="Acerca de", padding=10)
self.notebook.pack(padx=10, pady=10)
self.pack()
print(self.greeting_frame)
type(self.greeting_frame)
print(self.about_frame)
type(self.about_frame)
print(self.notebook)
type(self.notebook)
class GreetingFrame(ttk.Frame):
def __init__(self, *args, **kwargs):
print('Hola GreetingFrame')
super().__init__(*args, **kwargs)
self.name_entry = ttk.Entry(self)
self.name_entry.pack()
self.greet_button = ttk.Button(
self, text="Saludar", command=self.say_hello)
self.greet_button.pack()
self.greet_label = ttk.Label(self)
self.greet_label.pack()
def say_hello(self):
print(self.greet_button)
self.greet_label["text"] = \
"¡Hola, {}!".format(self.name_entry.get())
class AboutFrame(ttk.Frame):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.label = ttk.Label(self)
self.label["text"] = ("Visitanos en recursospython.com y "
"foro.recursospython.com.")
self.label.pack()
self.web_button = ttk.Button(self, text="Visitar web")
self.web_button.pack(pady=10)
self.forum_button = ttk.Button(self, text="Visitar foro")
self.forum_button.pack()
if __name__=="__main__":
main_window = tk.Tk()
app = Application(main_window)
app.mainloop()
PD: I have choosen "exec()" despite being a newbie to create the new tabs because I think it is better to create only the tabs you need instead of creating a predetermined and limited number of hidden tabs that are raised when the user wants to create a new tab. PD:尽管还是新手,我还是选择了“ exec()”来创建新选项卡,因为我认为最好只创建所需的选项卡,而不是创建预定数量有限的隐藏选项卡,这些隐藏选项卡会在用户需要时引发创建一个新标签。
The reason this is happening is because you have pack()
geometry manager which center aligns the widget. 发生这种情况的原因是因为您有
pack()
几何管理器,该管理器使小部件居中对齐。
Use grid()
geometry manager instead. 请改用
grid()
几何管理器。
Change this statement to self.pack()
self.grid(row=0, column=0)
将此语句
更改为self.pack()
self.grid(row=0, column=0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.