Can this be reversed (Tkinter)?

M

mark

I am looking at the format used by root.geometry(). The string returned
from root.geometry() is "%dx%d+%d+%d". Is there a quick and painless
way to extract the values from that string (i.e., "100x150+25+25" =>
(100,150,25,25))?

- Mark
 
J

John J. Lee

mark said:
I am looking at the format used by root.geometry(). The string returned
from root.geometry() is "%dx%d+%d+%d". Is there a quick and painless
way to extract the values from that string (i.e., "100x150+25+25" =>
(100,150,25,25))?

def geometry_tuple_from_string(text):
a, rest = text.split("x")
b, c, d = rest.split("+")
return tuple([int(x) for x in (a, b, c, d)])

print geometry_tuple_from_string("100x150+25+25")


John
 
D

Dave K

I am looking at the format used by root.geometry(). The string returned
from root.geometry() is "%dx%d+%d+%d". Is there a quick and painless
way to extract the values from that string (i.e., "100x150+25+25" =>
(100,150,25,25))?

- Mark

For that particular string, I can think of 2 one-liners:
s='100x150+25+25'
print tuple([int(x) for x in s.replace('x', '+').split('+')]) (100, 150, 25, 25)
print tuple([int(x) for y in s.split('x') for x in y.split('+')])
(100, 150, 25, 25)


For more general use, the regular expression "\d+" will match positive
integers:
s='100x150+25+25/(34*7);12-34*(56%78/9)'
import re
print tuple([int(x) for x in re.findall('\d+', s)])
(100, 150, 25, 25, 34, 7, 12, 34, 56, 78, 9)

Dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top