我试着这样放置小部件:

我不明白为什么我的代码没有做到这一点,试图在网上查找示例,但没有找到解决方案,我没有尝试使我更接近请求的结果.

到目前为止,这是我的代码(如果您对代码中的任何内容有任何评论,请随时告诉我,因为这是我首次尝试使用tkinter和一般的GUI):

from Tkinter import *
class box(object):
def __init__ (self, colour,s):
self.root = root
self.listbox = Listbox(self.root, fg = colour, bg = 'black')
self.s = s
self.place_scrollbar()
self.listbox.pack(side = self.s)
def place_scrollbar(self):
scrollbar = Scrollbar(self.root)
scrollbar.pack(side = self.s, fill = Y)
self.listbox.config(yscrollcommand = scrollbar.set)
scrollbar.config(command = self.listbox.yview)
def write(self, contenet):
self.listbox.insert(END, contenet)
root = Tk()
root.resizable(False, False)
boxs = Frame(root)
boxs.pack()
box.root = boxs
server = box("red", LEFT)
client = box("green", RIGHT )
bf = Frame(root)
bf.pack(side = BOTTOM)
entry = Entry(bf,bg ='black', fg = 'white')
entry.pack()
root.mainloop()
解决方法:
您can‘t do this,而仍使用pack,而仍然保持可调整大小,而无需使用其他框架来包含框对象.
但是在某些情况下,它可以组织得更好:通过使用父选项进行初始化,使用附加框架来包含框对象.
现在,box类中的小部件是全局根对象的子级.这实际上不是一个好习惯.因此,让我们首先传递并使用一个父对象用于内部的小部件.
更换:
def __init__ (self, colour,s):
self.root = root
self.listbox = Listbox(self.root, ...)
...
def place_scrollbar(self):
scrollbar = Scrollbar(self.root)
...
有:
def __init__ (self, parent, colour,s):
self.parent= parent
self.listbox = Listbox(self.parent, ...)
...
def place_scrollbar(self):
scrollbar = Scrollbar(self.parent)
...

这样一来,您现在就需要像下面这样初始化该对象:

server = box(root, "red", LEFT)
client = box(root, "green", RIGHT )

现在我们可以传递父窗口小部件,让我们创建一个包含它们的父框架.实际上,已经有一个未使用的框架,通过将其作为父级而不是root传递给盒子,框可以使用它:

server = box(boxs, "red", LEFT)
client = box(boxs, "green", RIGHT )

现在一切看起来都很好,如果愿意,可以选择使条目占据尽可能多的剩余空间,目前,将fill =’x’作为选项添加到条目包和包含它的框架中:

bf.pack(side = BOTTOM, fill='x')
...
entry.pack(fill='x')

您的整个代码应如下所示:

from Tkinter import *
class box(object):
def __init__ (self, parent, colour,s):
self.parent = parent
self.listbox = Listbox(self.parent, fg = colour, bg = 'black')
self.s = s
self.place_scrollbar()
self.listbox.pack(side = self.s)
def place_scrollbar(self):
scrollbar = Scrollbar(self.parent)
scrollbar.pack(side = self.s, fill = Y)
self.listbox.config(yscrollcommand = scrollbar.set)
scrollbar.config(command = self.listbox.yview)
def write(self, contenet):
self.listbox.insert(END, contenet)
root = Tk()
root.resizable(False, False)
boxs = Frame(root)
boxs.pack()
box.root = boxs
server = box(boxs, "red", LEFT)
client = box(boxs, "green", RIGHT )
bf = Frame(root)
bf.pack(side = BOTTOM, fill='x')
entry = Entry(bf,bg ='black', fg = 'white')
entry.pack(fill='x')
root.mainloop()

或:使用网格代替包(使用columnpan = 2选项输入).

一般答案

通常,可以通过以下方式将一个小部件并排放置在两个小部件之间:

用框架封装并排的窗口小部件,然后将框架简单地放在另一个窗口小部件上方:

try: # In order to be able to import tkinter for
import tkinter as tk # either in python 2 or in python 3
except ImportError:
import Tkinter as tk
def main():
root = tk.Tk()
side_by_side_widgets = dict()
the_widget_beneath = tk.Entry(root)
frame = tk.Frame(root)
for name in {"side b", "y side"}:
side_by_side_widgets[name] = tk.Label(frame, text=name)
side_by_side_widgets[name].pack(side='left', expand=True)
frame.pack(fill='x')
the_widget_beneath.pack()
root.mainloop()
if __name__ == '__main__':
main()
try: # In order to be able to import tkinter for
import tkinter as tk # either in python 2 or in python 3
except ImportError:
import Tkinter as tk
def main():
root = tk.Tk()
side_by_side_widgets = dict()
the_widget_beneath = tk.Entry(root)
for index, value in enumerate({"side b", "y side"}):
side_by_side_widgets[value] = tk.Label(root, text=value)
side_by_side_widgets[value].grid(row=0, column=index)
the_widget_beneath.grid(row=1, column=0, columnspan=2)
root.mainloop()
if __name__ == '__main__':
main()

在不使用其他帧的情况下,通过调用side_’bottom’作为第一个pack调用the_widget_beneath的pack,如Bryan’s comment中所示:

try: # In order to be able to import tkinter for
import tkinter as tk # either in python 2 or in python 3
except ImportError:
import Tkinter as tk
def main():
root = tk.Tk()
side_by_side_widgets = dict()
the_widget_beneath = tk.Entry(root)
the_widget_beneath.pack(side='bottom')
for name in {"side b", "y side"}:
side_by_side_widgets[name] = tk.Label(root, text=name)
side_by_side_widgets[name].pack(side='left', expand=True)
root.mainloop()
if __name__ == '__main__':
main()

通过创建全局main方法,然后在其中添加脚本主体并调用,您可以更轻松地注意到全局对象的可靠性.

...
def main():
root = Tk()
root.resizable(False, False)
boxs = Frame(root)
boxs.pack()
box.root = boxs
server = box(boxs, "red", LEFT)
client = box(boxs, "green", RIGHT )
bf = Frame(root)
bf.pack(side = BOTTOM, fill='x')
entry = Entry(bf,bg ='black', fg = 'white')
entry.pack(fill='x')
root.mainloop()
if __name__ == '__main__':
main()