【问题标题】:set Tkinter label widget below entry widget在入口小部件下方设置 Tkinter 标签小部件
【发布时间】:2016-08-08 15:23:46
【问题描述】:

我对 Tkinter 比较陌生,我正在尝试创建一个包含三个水平对齐的条目小部件和三个相应的标签小部件的 GUI。我正在努力解决如何将标签小部件放在条目小部件下方而不干扰输入框的对齐方式(即包装侧 = LEFT)。这是 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
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()

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

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

【问题讨论】:

    标签: python user-interface tkinter widget


    【解决方案1】:

    您可以为此使用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
    

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

    【讨论】:

    • 像魅力一样工作!唯一不能正常工作的是 Label(Sub, text = "Create x mesh nodes").grid(columnspan=3)。标签打印在三个条目上方。
    • 查看编辑,认为这就是您的意思 不确定您所说的“打印出三个条目上方”是什么意思
    猜你喜欢
    • 2014-03-18
    • 2019-12-29
    • 1970-01-01
    • 2017-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多