ttk.Notebook() 标签居中的问题

我正在尝试创建一个简单的 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):\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])))


PD:尽管我是创建新标签的新手,我还是选择了“exec()”,因为我认为最好只创建您需要的标签,而不是创建预定且数量有限的隐藏标签,这些标签在用户需要时会弹出创建一个新选项卡。


摇曳的蔷薇
浏览 247回答 1
1回答

潇湘沐

发生这种情况的原因是因为您有pack()几何管理器将小部件居中对齐。请改用grid()几何管理器。将此语句更改self.pack()为self.grid(row=0, column=0)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python