Tuesday 15 February 2011

class - Convert string into a Tkinter notebook frame -



class - Convert string into a Tkinter notebook frame -

ok trying find frame tkinter using, take width , height , resize window fits nicely without ugly spaces left. far have gotten following...

convert = {"tab1_name", "tab1"; "tab2_name", "tab2"; "tab3_name", "tab3") ##(it goes on) = mainframe.tab(mainframe.select(), "text") b = convert[a] w = b.winfo_reqwidth() h = b.winfo_reqheight() mainframe.configure(width=w, height=h)

the names of each frame in notebook tab1, tab2, tab3, etc., labels on them unique because describe happens in tab. want able take string returned convert dictionary function , utilize frame's name. not sure if frame class or else. there way convert string b frame's name , somehow utilize in .winfo_reqheight()? not want have create thing says...

if b=="tab1": w = tab1.winfo_reqwidth() h = tab1.winfo_reqheight() mainframe.configure(width=w, height=h)

for each frame because want easy add together new frames without having add together much code.

thank you

option 1:

you can store actual objects in dictionaries. try:

convert = {"tab1_name": tab1, "tab2_name": tab2, "tab3_name": tab3} = mainframe.tab(mainframe.select(), "text") b = convert[a] w = b.winfo_reqwidth() h = b.winfo_reqheight() mainframe.configure(width=w, height=h)

option 2: executing strings possible 'exec('arbitrary code in string')' function

see how execute string containing python code in python?.

you this: (with text in dictionary or whatever convert is)

convert = {"tab1_name": "tab1", "tab2_name": "tab2", "tab3_name": "tab3"} = mainframe.tab(mainframe.select(), "text") b = convert[a] code1 = "w = %s.winfo_reqwidth()" % b code2 = "h = %s.winfo_reqheight()" % b exec(code1) # python 2 is: exec code1 exec(code2) # python 3 changed exec statement function mainframe.configure(width=w, height=h)

be careful don't allow malicious code exec statement, because python run it. problem if end user can input things function(it sounds don't have worry this).

btw, think first line incorrect. open { close ). proper dictionary syntax be:

convert = {"tab1_name": "tab1", "tab2_name": "tab2", "tab3_name": "tab3"}

notice colons separating key , value, , commas in-between entries.

string class python-3.x tkinter frames

No comments:

Post a Comment