0

I've been having a lot of difficulties with this code, and I just can't find any solutions, I will post my code below.

from tkinter import *
a = []

class test(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.parent.title('testing')
        self.pack(fill=BOTH, expand=1)
        self.d = DoubleVar()
        self.d.set('None')

        def grab():
            b = ent.get()
            a.append(b)
            c = [s.strip('qwertyuiopasdfghjklzxcvbnm') for s in a]
            self.d.set(c[-1])
            if c[-1] == '':
                self.d.set('None')
        ent = Entry(self)
        ent.grid(row=0, column=0)
        but = Button(self, text='Get', command=grab)
        but.grid(row=1, column=0)
        Label(self, textvariable=self.d).grid(row=2, column=0)

root = Tk()
app = test(root)
root.mainloop

I guess my objective is to be able to ignore, or delete the letters that are placed inside of the entry box, as you can see, I've used the strip method, but it doesn't work the way I would like it to. If anyone could offer some advice, or a code, or link me to a question that I overlooked, that would be amazing, and I would be greatful.

EDIT: It already clears letters before and after, but nothing in between

4 Answers4

0

A little bit of lambda should do the trick

    a = "123a3456b"
    filter(lambda '0' <= x <= '9', a)
    print a
    "1233456"
lealhugui
  • 189
  • 1
  • 12
0

a clean way to do this is simply:

filter(lambda s: not str.isalpha(s), data)
acushner
  • 9,595
  • 1
  • 34
  • 34
0

A validator would be the correct pattern to solve this problem. Interactively validating Entry widget content in tkinter has an implementation in tkinter that you should be able to use.

Community
  • 1
  • 1
theorifice
  • 670
  • 3
  • 9
0

You're getting letters inside the numbers because you're putting the string into a list first.

strip() removes leading and trailing characters, so if you have a string: aaa000bbb111ccc, stripping letters from it will only remove the outer-most letters. If you split the string, however, and then strip letters from each element of the stripped string, you'll effectively remove all the letters. Then, you can join() the remaining parts of the list together to get back to your string. Consider this example:

>>> import string # string.ascii_letters returns a string of all letters (upper and lower), just easier than typing them
>>> def check(x):
    return ''.join([char.strip(string.ascii_letters) for char in x])

>>> var = 'aaa000bbb111ccc'
>>> var_as_list = [var]
>>> check(var)
'000111'
>>> check(var_as_list)
'000bbb111'

So, c should be:

c = ''.join([s.strip('qwertyuiopasdfghjklzxcvbnm') for s in b.get()])

You should also consider some further validation, if you want the field to only contain floats. Here's one method to trace any changes to a StringVar() instance and restrict changes to it to only being numbers and periods:

from tkinter import *
import string

def check(*args):
    # make a 'whitelist' of allowable characters
    whitelist = string.digits + '.'

    # set var to its current value - any characters not in whitelist
    var.set(''.join([i for i in var.get() if i in whitelist]))

root = Tk()

var = StringVar()
var.set('0.0')

Entry(root, textvariable=var).grid(row=0, column=0)
Label(root, textvariable=var).grid(row=1, column=0)

var.trace('w', check) # if var changes, call check()

mainloop()
atlasologist
  • 3,824
  • 1
  • 21
  • 35