首页 > 解决方案 > 如何在 Python Tkinter 中将条目移动到靠近标签的位置

问题描述

我是 python 和 Tkinter 的新手,我希望能做到 在此处输入图像描述

但我现在的输出是:

在此处输入图像描述

我尝试了不同的方法,但无法将带有黄色标签的条目框移动到靠近搜索的位置,并且标签Dictionary:与文本编辑器的级别不同。

from ttk import Combobox
from tkinter import *

class Init(object):
    def __init__(self):
        root.title('Sample')
        root.geometry('700x300')
        MainFrame()
        DictionaryFrame()

class MainFrame(object):
    def __init__(self):
        self.Textarea()

    def Textarea(self):
        self.txtareaframe = Frame(root)
        self.txtareaframe.grid(row=1, column=0, columnspan=2)
        self.scroll = Scrollbar(self.txtareaframe)
        self.scroll.pack(side=RIGHT, fill=Y)
        self.txtarea = Text(self.txtareaframe, wrap=WORD, yscrollcommand=self.scroll.set, height=10, width=50)
        self.txtarea.pack()
        self.scroll.config(command=self.txtarea.yview)

class DictionaryFrame(object):
    def __init__(self):
        self.dictionaryframe = Frame(root)
        self.dictionaryframe.grid(row=1, column=2, columnspan=2)

        self.emptylbl = Label(self.dictionaryframe, text='')  #This is just a empty space placeholder
        self.emptylbl.grid(row=1, column=3, padx=50)

        self.dictionarylbl = Label(self.dictionaryframe, text='Dictionary: ')
        self.dictionarylbl.grid(row=1, column=4, sticky="w")
        self.dictionarylist = Listbox(self.dictionaryframe)
        self.dictionarylist.grid(row=2, column=4)

        self.searchlbl = Label(self.dictionaryframe, text='Search: ')
        self.searchlbl.grid(row=3, column=4,sticky="w")
        self.searchinput = Entry(self.dictionaryframe, width=25)
        self.searchinput.grid(row=3, column=5)

root = Tk()
Init()
root.mainloop()

标签: python-3.xtkinter

解决方案


您所有的小部件都位于正确的位置。您只需要传递几个参数,主要是sticky方向:

from tkinter import *

class Init(object):
    def __init__(self):
        root.title('Sample')
        root.geometry('700x300')
        MainFrame()
        DictionaryFrame()

class MainFrame(object):
    def __init__(self):
        self.txtareaframe = Frame(root)
        self.txtareaframe.grid(row=1, column=0, columnspan=2, sticky="nesw") #1
        self.scroll = Scrollbar(self.txtareaframe)
        self.scroll.pack(side=RIGHT, fill=Y)
        self.txtarea = Text(self.txtareaframe, wrap=WORD, yscrollcommand=self.scroll.set, height=10, width=50)
        self.txtarea.pack(fill="both",expand=True) #2
        self.scroll.config(command=self.txtarea.yview)

class DictionaryFrame(object):
    def __init__(self):
        self.dictionaryframe = Frame(root)
        self.dictionaryframe.grid(row=1, column=2, columnspan=2, sticky="w") #3

        self.emptylbl = Label(self.dictionaryframe, text='')
        self.emptylbl.grid(row=1, column=3, padx=50)

        self.dictionarylbl = Label(self.dictionaryframe, text='Dictionary: ')
        self.dictionarylbl.grid(row=1, column=4, sticky="w")
        self.dictionarylist = Listbox(self.dictionaryframe)
        self.dictionarylist.grid(row=2, column=4, columnspan=2, sticky="ew") #4

        self.searchlbl = Label(self.dictionaryframe, text='Search: ')
        self.searchlbl.grid(row=3, column=4, sticky="w")
        self.searchinput = Entry(self.dictionaryframe, width=25)
        self.searchinput.grid(row=3, column=5)

root = Tk()
Init()
root.mainloop()

推荐阅读