我目前有一个具有以下布局的Tkinter GUI:
我想在我的上部条目小部件中创建多个矩形边框,所以它看起来像这样:
有没有办法做到这一点?
目前我的代码如下:
#!/usr/bin/python
from sys import exit
from Tkinter import *
from subprocess import Popen, PIPE
from time import sleep
from os.path import exists
import os, time, sys, ttk
#Initialize Tkinter by creating Tk root widget, which is a window with a
Root = Tk()
Root.title("GUI TOOL")
class Command:
def __init__(self, func, *args, **kw):
self.func = func
self.args = args
self.kw = kw
def __call__(self, *args, **kw):
args = self.args+args
kw.update(self.kw)
self.func(*args, **kw)
##Script definitions
def quitter():
exit(0)
##Button organization and layout
Sub0 = Frame(Root)
###Define X mesh node
Sub = Frame(Sub0)
#X mesh node
Label(Sub, text = "Create X mesh nodes").grid(row = 0, column = 1)
#X min text box area
v1=StringVar()
#Create entry widget to get user input, in this case a text string for the
X min, label min below entry widget box in appropriate column
Entry(Sub, textvariable=v1).grid(row=1, column=1)
Label(Sub, text = "min").grid(row=1, column=0)
#X max text box area
v2=StringVar()
#Create entry widget to get user input, in this case a text string for the
#X max, label max below entry widget box in appropriate column
Entry(Sub, textvariable=v2).grid(row=2, column=1)
Label(Sub, text = "max").grid(row=2, column=0)
#X step text box area
v3=StringVar()
#Create entry widget to get user input, in this case a text string for the
#X step increment, label step increment below entry widget box in
#appropriate column
Entry(Sub, textvariable=v3).grid(row=3, column=1)
Label(Sub, text = "step increment").grid(row=3, column=0, ipady = 5)
#Define line between X mesh node definitions and Y mesh node definitions
Sub.pack(side = TOP)
##Define Y mesh node
#Y mesh node
Label(Sub, text = "Create Y mesh nodes").grid(row = 0, column=3)
#Y min text box area
v4=StringVar()
#Create entry widget to get user input, in this case a text string for the
#Y min, label min below entry widget box in appropriate column
Entry(Sub, textvariable=v4).grid(row=1, column=3, ipadx = 5)
Label(Sub, text = "min").grid(row=1, column=2, ipadx = 5)
#Y max text box area
v5=StringVar()
#Create entry widget to get user input, in this case a text string for the
#Y max, label max below entry widget box in appropriate column
Entry(Sub, textvariable=v5).grid(row=2, column=3, ipadx = 5)
Label(Sub, text = "max").grid(row=2, column=2, ipadx = 5)
#Y step text box area
v6=StringVar()
#Create entry widget to get user input, in this case a text string for the
#Y step increment, label step increment below entry widget box in
#appropriate column
Entry(Sub, textvariable=v6).grid(row=3, column=3, ipadx = 5)
Label(Sub, text = "step increment").grid(row=3, column=2, ipady = 5, ipadx
= 5)
#Define line between X mesh node definitions and Y mesh node definitions
Sub.pack(side = TOP)
#Define Z mesh noe
#Z mesh node
Label(Sub, text = "Create Z mesh nodes").grid(row = 0, column = 5)
#Z min text box area
v7=StringVar()
#Create entry widget to get user input, in this case a text string for the
#Z min, label min below entry widget box in appropriate column
Entry(Sub, textvariable=v7).grid(row=1, column=5, ipadx = 5)
Label(Sub, text = "min").grid(row=1, column=4, ipadx = 5)
#Z max text box area
v8=StringVar()
#Create entry widget to get user input, in this case a text string for the
Z max, label max below entry widget box in appropriate column
Entry(Sub, textvariable=v8).grid(row=2, column=5, ipadx = 5)
Label(Sub, text = "max").grid(row=2, column=4, ipadx = 5)
#Z step text box area
v9=StringVar()
#Create entry widget to get user input, in this case a text string for the
#Z step increment, label step increment below entry widget box in
#appropriate column
Entry(Sub, textvariable=v9).grid(row=3, column=5, ipadx = 5)
Label(Sub, text = "step increment").grid(row=3, column=4, ipady = 5, ipadx
= 5)
#Define line between Z mesh node definitions and Mesh button definitions
Sub.pack(side = TOP)
Frame(Sub0,height=2, relief = GROOVE, bg="black").pack(side = TOP, fill =
BOTH, pady= 5)
###Define Mesh Interval buttons
Sub3 = Frame(Sub0)
#Generate Evenly Spaced Mesh Button
Button(Sub3, text = "Generate Evenly Sampled Interval Mesh").grid(row=10,
column = 0, ipady = 2)
#Generate Geometrically Spaced Mesh Button
Button(Sub3, text = "Generate Geometrically Sampled Interval
Mesh").grid(row=11, column = 0, ipady = 2)
#Generate Irregularly Spaced Mesh Button
Button(Sub3, text = "Generate Irregularly Sampled Interval
Mesh").grid(row=12, column = 0, ipady = 2)
#Define line between Mesh buttons and Quit button
Sub3.pack(side = TOP)
Frame(Sub0,height=2, relief = GROOVE, bg="black").pack(side = TOP, fill =
BOTH, pady= 5)
##GUI organization and quit button
Sub0.pack(side = TOP, padx = 10)
Button(Root, text = "Quit", command = quitter).pack(side = BOTTOM, pady =
5)
#The window will not appear until we enter the Tkinter event loop. Script
Root.mainloop()
提前致谢!!!
答案 0 :(得分:0)
最简单的方法是将这些包装在一个框架中,将背景颜色设置为"黑色"并提供框架padx
和pady
答案 1 :(得分:0)
“如何在一组窗口小部件周围放置框架”的简单答案是使用框架窗口小部件,并将这些内部窗口小部件放在该框架内。在这种情况下,您可以为“创建X网格节点”创建一个框架,为“创建Y网格节点”创建一个框架,为“创建Z网格节点”创建一个框架
由于这些帧的内容基本相同(相同数量的标签和条目),因此它们应该很好地排列。
另一种选择是将所有这些标签和条目放在一个框架中,并将该框架的颜色设置为“黑色”。然后,通过正确使用填充选项,您可以对其进行排列,以便背景颜色在组之间渗透。
专业提示:如果将所有布局命令组合在一起而不是组合窗口小部件创建和窗口小部件布局,这些更改将变得更容易 ,并且代码可以说更容易理解。
例如,而不是:
Entry(Sub, textvariable=v2).grid(row=2, column=1)
Label(Sub, text = "max").grid(row=2, column=0)
...
Entry(Sub, textvariable=v3).grid(row=3, column=1)
Label(Sub, text = "step increment").grid(row=3, column=0, ipady = 5)
...
这样做:
e1 = Entry(Sub, textvariable=v2)
e2 = Entry(Sub, textvariable=v3)
...
l1 = Label(Sub, text = "step increment")
l2 = Label(Sub, text = "max")
...
e1.grid(row=2, column=1)
l1..grid(row=2, column=0)
e2.grid(row=3, column=1)
l2.grid(row=3, column=0, ipady = 5)