ttk出现问题.记事本()选项卡居中



我正试图创建一个简单的GUI来解决结构问题。

目的是让用户在GUI中添加选项卡,如果他/她想启动另一个项目,即使选项卡已经打开。

我现在的主要问题是,当根据以下代码创建选项卡时,它是居中的,而不是从左到右创建:

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):ntdef __init__(self, *args, **kwargs):nttsuper().__init__(*args, **kwargs)'.format(self.tab_names[how_many_tabs]))
else:
self.tab_names[0] = 'Tab1'
exec('global {0}nclass {0}(ttk.Frame):ntdef __init__(self, *args, **kwargs):nttsuper().__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()

我用这个代码写的:

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:尽管我是一个新手,但我还是选择了"exec(("来创建新的选项卡,因为我认为最好只创建你需要的选项卡,而不是创建预先确定的、数量有限的隐藏选项卡,当用户想要创建新选项卡时,这些选项卡会被弹出。

之所以会发生这种情况,是因为您有pack()几何体管理器,它将小部件居中对齐。

请改用grid()几何图形管理器。

将此语句self.pack()更改为self.grid(row=0, column=0)

最新更新