[Tutor] Question about why a list variable is apparently global.
boB Stepp
robertvstepp at gmail.com
Thu Nov 27 00:23:40 CET 2014
Python 2.4.4
Solaris 10
#!/usr/bin/env python
from Tkinter import *
def printLabel():
print "Button number ", var.get(), " was pressed."
print "You selected this option:", l[var.get() - 1][0]
root = Tk()
root.title("ROI List Creator")
root.geometry(newGeometry='225x230+900+300')
root.tk_setPalette(background='gray')
buttonNumber = []
l = [("Brain_Partial", 1), ("Brain_Whole", 2),
("Head & Neck", 3), ("Chest", 4), ("Breast_Whole", 5),
("Breast_Partial", 6), ("Abdomen", 7), ("Pelvis", 8),
("Prostate", 9)]
var = IntVar()
for text, value in l:
buttonNumber.append(Radiobutton(root, text = text, value = value,
command=printLabel, variable = var).pack(anchor=W))
var.set(5)
print "The button's value is: ", var.get()
root.update()
print "The geometry info is: ", root.winfo_geometry()
print "The screen width is: ", root.winfo_screenwidth()
print "The screen height is: ", root.winfo_screenheight()
root.mainloop()
First, I continue to "Easter Egg Hunt" in the sweet land of Python,
and now Tkinter. So what I actually know is quite a hodgepodge at this
time.
The function above, printLabel(), will eventually read a file of names
anatomical structures based upon the user's radiobutton selection and
populate those names into a ROI List in our planning software. But for
now I am mostly focused on designing the small radiobutton window and
getting it to do what I expect. I am totally new to Tkinter, but it
seems much more manageable than what our planning software provides
for scripting GUIs.
First question: How can the printLabel() function see the list
variable, l, defined outside of this function? I thought that
functions only had access to variables local to the function and
whatever else is passed to it during the function call.
Second: Will the statements near the end that return the display
screen's width and height be a reliable way of determining the user's
actual monitor settings? My intent is to use this information to
appropriately scale and position windows; not only in this particular
simple window, but in others to come which will potentially be large
and complex. I have tested this on two different sized monitors where
I work and so far it has given the correct results.
Third: I am always open to stylistic comments and how to be more pythonic!
Fourth: And perhaps this should go into a new thread, I am not sure I
understand the full intent and use of Tkinter's Variable class. Any
clarification on this would be welcome as well.
Thanks in advance!
--
boB
More information about the Tutor
mailing list