python-3.x - 如何在 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()
解决方案
您所有的小部件都位于正确的位置。您只需要传递几个参数,主要是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()
推荐阅读
- python - 如何训练项目序列分割模型
- django - 在 Django 中一次更新其中一个时,如何反映两个一对多关系实例的更改?
- php - 照亮查询生成器,如何将日期转换为 Carbon 对象
- python - 如何获取 df1 中存在但 df2 中不存在的行,反之亦然,同时忽略列“值”
- python - 我怎样才能正确循环这个乘法?
- production - 将此元素嵌入到另一个 angular9.1.1 项目后,Angular(9.1.1) 元素出现错误
- python-3.x - 熊猫在情节中下降了行
- r - 在 R 脚本中,代码运行,但是一旦我 knitr 它抛出一个错误,我无法想象可能是什么问题
- php - 致命错误:通过 wordpress 插入数据时在 null 上调用成员函数 prepare()
- php - PHP 没有为月减法提供正确的输出