如何使 tkinter 条目默认值永久 [英] How to make a tkinter entry default value permanent

查看:35
本文介绍了如何使 tkinter 条目默认值永久的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 python 编写一个程序,该程序将采用特定格式、电话号码和美元/美分值.如何让 tkinter 具有永久的默认值,不可删除.例如 (XXX)-XXX-XXXX?

I am writing a program in python that will take in specific formats, a Phone number and dollar/cent values. How can I make tkinter have default value which is permanent, not deletable. For example (XXX)-XXX-XXXX?

基本上你可以向小部件添加一个条目,但该条目被定义为永久值,例如当它为空时它看起来像 (_ _ _)-___-____ 当它有文本时它看起来像 (434)-332-1234

basically you can add an entry to the widget but the entry is defined the permanent value like when its empty it looks like (_ _ _)-___-____ when it has text it looks like (434)-332-1234

推荐答案

如果我理解正确,您需要某种模板,用户可以在其中键入他/她的信息,但仅限于某种格式.您可以使用条目的 validatecommand 执行此操作.基本上,这会在插入某些内容时调用一个函数,并且可以返回 True 或 False 以接受或拒绝更改.如需详细了解其工作原理,请参阅 Bryan Oakley 的回答.

If I understand you correctly, you want some sort of template in which a user can type his/her information but is restricted to some format. You can do this using the Entry's validatecommand. Basically, this calls a function whenever something is being inserted and can return True or False to accept or reject the change. For more information about how this works see this answer by Bryan Oakley.

在您的情况下,您希望函数在具有 (...)-...-.... 格式的内容时返回 True,您可以使用常规检查表达.您可以使用的正则表达式是 ^\(\d{0,3}\)-\d{0,3}-\d{0,4}$.
我会为你解释的.^ 表示应该是字符串的开头,\( 表示应该有一个 (, \d{0,3} 表示可以有 0 到 3 个数字(我假设你只想要数字,如果不是你可以把它改成 \w 来接受任何字母或数字).然后是 \) 意思是 ),一个 - 字面意思是 -,一些数字和一个 - 并在最后一个 $ 这意味着它应该是字符串的结尾.

In your case, you'd want the function to return True whenever something has the format (...)-...-...., which you can check with a regular expression. The regular expression you could use is ^\(\d{0,3}\)-\d{0,3}-\d{0,4}$.
I'll explain it for you. ^ means that that should be the beginning of the string, \( means there should be a (, \d{0,3} means that there can be 0 to 3 numbers (I assumed you only want numbers, if not you can change it to \w to accept any letter or number). Then comes \) which means ), a - which literally means -, some digits and a - again and at the end a $ which means that should be the end of the string.

您可以在 validatecommand 函数中使用此正则表达式来检查条目是否具有正确的格式:

You can use this regular expression in the validatecommand function to check if the entry has the right format by using:

import Tkinter as tk
import re

class MyApp():
    def __init__(self):
        self.root = tk.Tk()

        vcmd = (self.root.register(self.OnValidate), '%P')
        self.entry = tk.Entry(self.root, validate="key", 
                              validatecommand=vcmd)
        self.entry.pack()
        self.prog = re.compile('^\(\d{0,3}\)-\d{0,3}-\d{0,4}$')
        self.entry.insert(0, '()--')
        self.root.mainloop()

    def OnValidate(self, P):
        if self.prog.match(P):
            result = True
        else:
            result = False

        return result

app=MyApp()

我使用之前链接的答案作为模板,删除了您的特定情况不需要的所有内容并插入了正则表达式.因为它只在字符串匹配模式时返回 True,所以只允许符合模式的编辑.

I used the before linked answer as template, removed everything that you don't need for your particular case and inserted the regular expression. Because it only returns True when the string matches the pattern, only edits that fit in the pattern are allowed.

这篇关于如何使 tkinter 条目默认值永久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆