Tkinter小部件在同一行中,但为什么不这样显示呢?

时间:2019-07-24 15:37:35

标签: python tkinter ttk

我正在尝试为搜索引擎创建开始菜单。我正在尝试在标签旁边显示按钮和其他小部件。这是我的代码:

class Application(tk.Tk): #subclass Tk
    '''Does most of the handling.'''
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.title("Academic Search")
        self.geometry("1000x600")
        self.resizable(width=True,height=True)
        self.start = Start(self)
        self.start.grid(sticky=(tk.E + tk.W + tk.N + tk.S))
        self.update_button = ttk.Button(self, text="Update Database", command=self.to_database)
        self.update_button.grid(row=0,column=0,sticky=tk.W)
        self.cc_button = ttk.Button(self, text="Find Common Citations",command=self.to_cc)
        self.cc_button.grid(row=1, column=0, sticky=tk.W)#, pady=20)
        self.search_button = ttk.Button(self, text="Search", command=self.search)
        self.search_button.grid(row=2, column=2, sticky=(tk.E))


class Start(tk.Frame): #subclass of Frame
    '''The start window'''
    def __init__(self, parent, *args, **kwargs): #takes same args as parent
        '''Initializes start window.'''
        super().__init__(parent, *args, **kwargs) #calls superclass constructor
        query = tk.StringVar()
        self.num_return = tk.IntVar()
        cw = tk.DoubleVar()
        cd = tk.IntVar()
        graph = tk.BooleanVar()
        jstor = tk.BooleanVar()
        self.mb_text = tk.StringVar()
        self.mb_text.set('Search multiple DOIs')
        self.num_return = tk.IntVar()
        self.search_box = ttk.Entry(parent, text='Enter one DOI:',textvariable=query)
        self.multiple_button = ttk.Button(self, textvariable=self.mb_text,command=self.to_mult)
        self.lbl = ttk.Label(self,text='Enter one DOI.')
        self.num_return_lbl = ttk.Label(self, text='Maximum number of records returned:')
        self.num_return_box = ttk.Entry(parent, textvariable=self.num_return)
        self.cw_lbl = ttk.Label(self, text='Citation weight (must be between 0 and 1):')
        self.cw_box = ttk.Spinbox(parent, from_=0, to=1, increment=0.01,textvariable=cw)
        self.cd_lbl = ttk.Label(self, text='Maximum citation distance (runtime will increase dramatically if greater than 3):')
        self.cd_box = ttk.Spinbox(parent, from_=1, to=10,increment=1, textvariable=cd)
        self.jstor_button = ttk.Checkbutton(parent, text='Use JSTOR (requires JSTOR Labs token)',command=jstor)
        self.graph_box = ttk.Checkbutton(parent, text='Graph', variable=graph)
        self.add_widgets()

    def add_widgets(self):
        '''Adds widgets to grid.'''
        self.lbl.grid(row=2, column=1, sticky=tk.E)
        self.cw_lbl.grid(row=3, column=1, sticky=tk.E)
        self.cw_box.grid(row=3, column=2, sticky=tk.E)
        self.cd_lbl.grid(row=4, column=1, sticky=tk.E)
        self.cd_box.grid(row=4, column=2, sticky=tk.E)
        self.num_return_lbl.grid(row=5, column=1, sticky=tk.E)
        self.num_return_box.grid(row=5, column=2, sticky=tk.E)
        self.search_box.grid(row=2, column=2, sticky=(tk.E+tk.W))
        self.graph_box.grid(row=6, column=2, sticky=tk.E)
        self.jstor_button.grid(row=7, column=2, sticky=tk.E)
        self.multiple_button.grid(row=9, column=0, sticky=(tk.W+tk.N))

为什么标签在同一行中时不与其他元素(旋转框,复选框,条目)对齐?

这是我做mainloop()时的样子:

screenshot of mainloop() running

1 个答案:

答案 0 :(得分:3)

按钮和标签的父母不同,这就是为什么它们出现在不同位置的原因。

如果为Start框架赋予独特的颜色,则可以非常清楚地看到这一点。例如,将创建Start的方式更改如下:

self.start = Start(self, background="red")

您最终将看到一个如下所示的窗口:

enter image description here

在这两行代码的每一行中,查看第一个参数。一个使用self,另一个使用parentselfparent是两个不同的小部件。

    self.cw_lbl = ttk.Label(self, ...)
    self.cw_box = ttk.Spinbox(parent, ...)

parent中使用Start作为窗口小部件的父级的任何地方,都应将其更改为self。例如,上面的代码需要更改为:

self.cw_box = ttk.Spinbox(self, ...)