How to make a tkinter entry default value permanent

2024/11/16 19:36:13

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?

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

Answer

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.

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.

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 reclass 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 = Trueelse:result = Falsereturn resultapp=MyApp()

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.

https://en.xdnf.cn/q/120291.html

Related Q&A

distribute value in buckets

Consider below DF, I have an input number=4 to be inserted evenly in different hour buckets.p_hourly mins 0 2020-09-10 07:00:00 60.0 1 2020-09-10 08:00:00 60.0 2 2020-09-10 09:00:00 60…

for loop over list break and continue

To specify the problem correctly :i apologize for the confusion Having doubts with breaking early from loop . I have folders - 1995,1996 to 2014 . Each folder has xml files. In some xml files the entr…

ImportError: cannot import name loads from json (unknown location)

Previos title was: AttributeError: module json has no attribute loads I changed it because it looks similar to this but at the link that i provided, the problem seems that the person was having a file…

How can I filter the domains served by a CDN from a list of domain names?

I have a list of domains and I need to filter the domains served by a CDN(Content Delivery Network). I am going to use python script to do that. At the first I was thinking I can identify them from the…

Convert int(round(time.time())) to C# [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

How to iterate over all elements of a 2D matrix using only one loop using python

I know you can iterate over a 2d matrix using two indexes like this: import numpy as npA = np.zeros((10,10))for i in range(0,10):for j in range(0,10):if (i==j):A[i,j] = 4Is there a way of doing this us…

Parse table names from a bunch SQL statements

I have an table with thousands of SQL statements in a column called Queries. Any ideas on how to get just the table names from the statements by using a regular expression?

click multiple buttons with same class names in Python

This a column in a table this column contains buttons, on pressing each buttons a pdf is downloadedThe buttons have the same class names and I want to click on all the buttons.This is what I did, but i…

Python equivalent to subset function in r [duplicate]

This question already has answers here:subsetting a Python DataFrame(6 answers)Closed 4 years ago.I dont know python at all but the project Im currently working on must be done using it, I have this r …

Force subprocess to use Python 3 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…