在条目小部件下面设置Tkinter标签小部件

时间:2016-04-16 18:02:03

标签: python user-interface tkinter widget

我对Tkinter相对较新,我正在尝试创建一个带有三个水平对齐的条目小部件和三个相应标签小部件的GUI。我正在努力解决如何将标签小部件放在条目小部件下面而不干扰输入框的对齐(即包装侧=左侧)。以下是GUI的示例快照,没有输入框下方的标签:

enter image description here

目前我的脚本看起来像这样,标签小部件被注释掉了:

#!/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
import time
import sys
import ttk

#Initialize Tkinter by creating Tk root widget, which is a window with a
title bar. The root widget has to be created before any other widgets.

Root = Tk()
Root.title("GUI")

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)
Sub = Frame(Sub0)

#X mesh node
Label(Sub, text = "Create x mesh nodes").pack(side = TOP, pady = 5)

#x min text box area
v1=StringVar()
#Create entry widge to get user input, in this case a text string for the x      
min mesh

Entry(Sub, textvariable=v1).pack(side = LEFT, pady = 5, padx = 5)
#Label(Sub, text = "min").pack(side = BOTTOM, pady = 5, padx = 5)


#x max text box area
v2=StringVar()
Entry(Sub, textvariable=v2).pack(side = LEFT, pady = 5, padx = 5)
#Label(Sub, text = "max").pack(side = BOTTOM, pady = 1)

#x step text box area
v3=StringVar()
Entry(Sub, textvariable=v3).pack(side = LEFT, pady = 5, padx = 5)
#Label(Sub, text = "step increment").pack(side = LEFT, pady = 1)

Sub.pack(side = TOP)
Frame(Sub0,height=1, relief = GROOVE, bg="black").pack(side = TOP, fill = 
BOTH, pady= 5)


#GUI Organization#


Sub0.pack(side = TOP, padx = 10)

Button(Root, text = "Quit", command = quitter).pack(side = TOP, pady = 5)

#The window will not appear until we enter the Tkinter event loop. Script
will remain in the event loop until we close the window.

Root.mainloop()

当我取消注释标签小部件时,我得到以下内容:

enter image description here

有人可以指导我如何实现这一目标吗?或者建议一个更好的方法来实现它?还有,有办法控制条目小部件的大小?我希望他们变小。提前谢谢!

1 个答案:

答案 0 :(得分:2)

您可以使用grid几何管理器:

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("VIZ 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)
Sub = Frame(Sub0)

#X mesh node
Label(Sub, text = "Create x mesh nodes").grid(columnspan=3)

#x min text box area
v1=StringVar()
#Create entry widge to get user input, in this case a text string for the x      

Entry(Sub, textvariable=v1).grid(row=1, column=0)
Label(Sub, text = "min").grid(row=2, column=0)


#x max text box area
v2=StringVar()
Entry(Sub, textvariable=v2).grid(row=1, column=1)
Label(Sub, text = "max").grid(row=2, column=1)
#x step text box area
v3=StringVar()
Entry(Sub, textvariable=v3).grid(row=1, column=2)
Label(Sub, text = "step increment").grid(row=2, column=2)

Sub.pack(side = TOP)
Frame(Sub0,height=1, relief = GROOVE, bg="black").pack(side = TOP, fill = 
BOTH, pady= 5)


#GUI Organization#


Sub0.pack(side = TOP, padx = 10)

Button(Root, text = "Quit", command = quitter).pack(side = TOP, pady = 5)

#The window will not appear until we enter the Tkinter event loop. Script

Root.mainloop()

对于条目的大小,您可以执行以下操作:

Root.option_add("*Entry.Width", 5) #10 is default

使用这样的选项添加将覆盖所有条目小部件的大小,您可以使用width上的Entry(....., width = *, ...)关键字指定个人大小