Python Forum
[Tkinter] How to restrict characters used in an Entry widget?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How to restrict characters used in an Entry widget?
#2
200+ lines of code for one simple call is more than I want to go through, so this is a simple example of using trace to validate code/
try:
    import Tkinter as tk     ## Python 2.x
    import ttk
except ImportError:
    import tkinter as tk     ## Python 3.x
    from tkinter import ttk

class Converter:
    def __init__(self, top):
        self.vars = tk.StringVar()
        self.vars.trace('w', self.validate)
        self.Entry1 = tk.Entry(top, textvariable = self.vars,
                               width=20).grid()

    def validate(self, *args):
        if not self.vars.get().isalpha():
            corrected = ''.join(filter(str.isalpha, self.vars.get()))
            self.vars.set(corrected)
 
root=tk.Tk()
C=Converter(root)
root.mainloop() 
Reply


Messages In This Thread
RE: How to restrict characters used in an Entry widget? - by woooee - Jun-20-2018, 03:44 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: could not convert string to float: '' fron Entry Widget russellm44 5 1,249 Mar-06-2024, 08:42 PM
Last Post: russellm44
  [Tkinter] entry widget DPaul 5 1,789 Jul-28-2023, 02:31 PM
Last Post: deanhystad
  Tkinter Exit Code based on Entry Widget Nu2Python 6 3,269 Oct-21-2021, 03:01 PM
Last Post: Nu2Python
  method to add entries in multi columns entry frames in self widget sudeshna24 2 2,429 Feb-19-2021, 05:24 PM
Last Post: BashBedlam
  Entry Widget issue PA3040 16 7,282 Jan-20-2021, 02:21 PM
Last Post: pitterbrayn
  [Tkinter] password with Entry widget TAREKYANGUI 9 6,442 Sep-24-2020, 05:27 PM
Last Post: TAREKYANGUI
  [Tkinter] Get the last entry in my text widget Pedroski55 3 6,685 Jul-13-2020, 10:34 PM
Last Post: Pedroski55
  How to retreive the grid location of an Entry widget kenwatts275 7 4,958 Apr-24-2020, 11:39 PM
Last Post: Larz60+
  POPUP on widget Entry taratata2020 4 3,968 Mar-10-2020, 05:04 PM
Last Post: taratata2020
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 4,581 Jan-23-2020, 09:00 PM
Last Post: HBH

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020