在文本框旁边而不是在消息框中显示输出?

时间:2014-07-18 11:02:49

标签: python tkinter

import Tkinter
import tkMessageBox

class Values(Tkinter.Tk):
    """docstring for Values"""
    def __init__(self, parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()

        self.Val1Lbl = Tkinter.Label(self,text="Value 1")
        self.Val1Lbl.grid(row=0,column=0)

        self.Val1Txt = Tkinter.Entry(self)
        self.Val1Txt.grid(row=0,column=1)
        self.err_l1 = Tkinter.Label(self, text='', fg='red')
        self.err_l1.grid(row=0,column=2)

        self.val1 = None
        self.val2 = None

        SubmitBtn = Tkinter.Button(self, text="Submit",command=self.hide_label)
        SubmitBtn.grid(row=1,column=2)


    def hide_label(self, event=None):
            self.val1=self.Val1Txt.get()
            if self.val1.strip() == '':
               self.err_l1[''] = 'error'

我想在文本框旁边显示错误消息,如何在单击提交按钮后触发标签 我是python的新手,所以任何人都可以帮助我..!

1 个答案:

答案 0 :(得分:0)

您可以使用如下定义的标签..

Label(root, text = err_msg, fg='red').place(x = entry.x + entry.width + 10, y = entry.y)

更好的是,您可以将标签放在第一位,隐藏(或者只是没有文字),然后使用它以红色显示消息..

err_l1 = Label(root, text='', fg='red') # the empty text is there only so that we can do err_l1['text'] = err_msg

# now in the submit button click event handler ..

if entry.get().strip() == '':
    err_l1['text'] = err_msg # as simple as that ;)