Validating Tkinter Entry Box

2024/11/15 17:58:08

I am trying to validate my entry box, that only accepts floats, digits, and operators (+×÷-, %). But my program only accepts numbers not symbols.

I think it is a problem with my conditions or Python Regex.

Here's the code:

from tkinter import *
import reroot = Tk()
def correct(inp):pattern = re.compile(r'^(\d*\.?\d*)$')if pattern.match(inp) is not None:return Trueelif inp is "":return Trueelse:return Falsea = Entry(root)
e = root.register(correct)
a.config(validate='key', validatecommand=(e, '%P'))
a.pack()root.mainloop()
Answer

Your regexp only matches floats: \d*.?\d* matches digits followed optionally by a dot and more digits. If what you want to match is a pattern like [float][operator][float], then you can use (just add the operators you want in the square brackets):

pattern = re.compile(r'^\d*\.?\d*[+*/\-%]?\d*\.?\d*$')

If you don't care about the order and just want to allow any sequence of numbers and operators:

pattern = re.compile(r'^[\d.+*/\-%]*$')
https://en.xdnf.cn/q/119029.html

Related Q&A

Imaplib with GMail offsets uids

Im querying my gmail inbox using pythons ImapLib with a range parameter, but my returned uids are offset from what I request. My request is as follows:M = imaplib.IMAP4_SSL(imap.gmail.com) M.login(USER…

Accessing a folder containing .wav files [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.This question was caused by a typo or a problem that can no longer be reproduced. While similar q…

What is the right Python idiom for sorting by a single criterion (field or key)? [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 8…

Incorrect checking of fields in list using a for loop

I have the following code that seeks to read the file contents into a list (this bit works) and then display a message of acceptance, IF the bank details (user and corresponding number) matches. e.g. i…

Why myVar = strings.Fields(scanner.Text()) take much more time than comparable operation in python?

Consider the following code in golangnow := time.Now() sec1 := now.Unix()file, err := os.Open(file_name) if err != nil {log.Fatal(err) } defer file.Close()scanner := bufio.NewScanner(file)var parsedLin…

When reading an excel file in Python can we know which column/field is filtered

I want to capture the field or column name that is filtered in the excel file when reading through python. I saw that we can also capture only the filtered rows by using openpyxl and using hidden == Fa…

Error:__init__() missing 1 required positional argument: rec

I am new to python. I am trying to do microphone file that ought to detect, listen, record and write the .wav files. However, it is giving me an error while I am trying to run the file. It is saying:Ty…

Maya: Connect two Joint chains with Parent Constraint

So here is a snipit of an IK spine builder Ive been working on. Ive figure out how to make lists to duplicate the bound into an IK chain, what Ive got stuck on however is I want my list and for loop to…

What is the equivalent for onkeydown and onkeyup (Javascript events) in python?

There are events called onkeydown and onkeyup in Javascript. Can anyone please suggest the python equivalent of it?

Matching number string pairs

I have the following sample string:R10666: 273141 C1 + 273141 C2 + 273141 C3 + 273141 C4 + 273141 C5 - 273141 C6I want to obtain:[(273141,C1), ..., (- 273141, C6)]The numbers can be floating point numb…