473,322 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

problem with tkinter

hello.

the following code:

1 from Tkinter import *
2
3 class MiaApp:
4 def __init__(self, genitore):
5 self.mioGenitore = genitore
6 self.i = IntVar()
7 self.i.set(42)
8 self.s = StringVar()
9 self.s.set("Baobab")
10 self.lab = {}
11 self.lab["self.i"] = Label(self.mioGenitore)
12 self.lab["self.i"].configure(width = 30, relief = RIDGE,
13 text = "[vuota]")
14 self.lab["self.i"].pack()
15 self.lab["self.s"] = Label(self.mioGenitore)
16 self.lab["self.s"].configure(width = 30, relief = RIDGE,
17 text = "[vuota]")
18 self.lab["self.s"].pack()
19 self.but = Button(self.mioGenitore)
20 self.but.configure(text = "Vai!", command = self.procedi)
21 self.but.pack()
22 def procedi(self):
23 for var in ("self.i", "self.s"):
24 self.lab[var].configure(textvariable = var)
25
26 radice = Tk()
27 miaApp = MiaApp(radice)
28 radice.mainloop()

is intended to make a window with 2 labels and a button, such that
pressin the button you get the labels display the content of two
variables. it does not work, of course, as intended. is there anybody
who can point me in the right direction? (the problem seems to be that
the command option wants a variable name, not a string containing that
name).

hopefully

macs
Jul 18 '05 #1
5 2020
Instead of indexing self.lab by strings, you can index them by the
attributes themselves : self.lab[self.i], and change line 23 into

for var in (self.s, self,i)

For your example, I wouldn't have used the "text" option in the
definition of the labels, then "textvariable" in the callback method
(procedi) ; I would have used only "text" and no IntVar or StringVar,
just an integer and a string :

class MiaApp:
def __init__(self, genitore):
self.mioGenitore = genitore
self.i = 42
self.s = "Baobab"
self.lab = {}
self.lab[self.i] = Label(self.mioGenitore)
self.lab[self.i].configure(width = 30, relief = RIDGE,
text = "[vuota]")
self.lab[self.i].pack()
self.lab[self.s] = Label(self.mioGenitore)
self.lab[self.s].configure(width = 30, relief = RIDGE,
text = "[vuota]")
self.lab[self.s].pack()
self.but = Button(self.mioGenitore)
self.but.configure(text = "Vai!", command = self.procedi)
self.but.pack()
def procedi(self):
for var in (self.i, self.s):
self.lab[var].configure(text = var)

Regards,
Pierre
Jul 18 '05 #2
On Tue, 29 Mar 2005 22:32:59 +0200, Pierre Quentel <qu************@wanadoo.fr> wrote:
Instead of indexing self.lab by strings, you can index them by the
attributes themselves : self.lab[self.i], and change line 23 into

for var in (self.s, self,i)
I really think this is asking for trouble: I suppose that the i and s attributes are meant to change at some point in the future, and you're mapping their *values* to the corresponding labels. So if the value changes, you won't be able to get the label again.
For your example, I wouldn't have used the "text" option in the
definition of the labels, then "textvariable" in the callback method
(procedi) ; I would have used only "text" and no IntVar or StringVar,
just an integer and a string :


I would have done exactly the contrary, as it is far more easier to use. Using the textvariable option in Label's does not require you to remember the labels, but only the variables used to hold their contents. I'd also use two mappings: the first mapping a name to a Tkinter variable for the label variables, and the second for the values to give to these variables later.

Here is my version of the class code:

class MiaApp:
def __init__(self, genitore):
self.mioGenitore = genitore
## Mapping for variables
self.variables = {
"i" : StringVar(),
"s" : StringVar()
}
## Mapping for future variable values
self.values = {
"i" : 42,
"s" : "Baobab"
}
## Now, create the labels
for var in self.variables.values():
## Default text
var.set("[vuota]")
## Create label
lb = Label(self.mioGenitore, width=30, relief=RIDGE, textvariable=var)
lb.pack()
## The button is no more remembered in an attribute, as it does not seem to be needed
but = Button(self.mioGenitore, text = "Vai!", command = self.procedi)
but.pack()

def procedi(self):
## Just change the variable values
for varName in self.variables.keys():
self.variables[varName].set(self.values[varName])

Note that I only used StringVar's: since the variable holds the *text* for a Label, it is not a good idea to use anything else. I tend to use IntVar, BooleanVar or DoubleVar only when I'm sure it won't ever be set to anything else than an integer, a boolean or a float respectively. So for Label and Entry text variables, I always use a StringVar. In spite of their name, these variable can actually hold anything; you will just get the value you've given to them as text rather than as their original type:
v = StringVar()
v.set(42)
v.get()

'42'

So if you want to get the integer back, you'll need to convert the value explicitely. This is in fact what is done by Tkinter when using an IntVar, but by doing it yourself, you'll be able to decide what to do if the conversion fails instead of just getting a generic TclError.

HTH
--
python -c 'print "".join([chr(154 - ord(c)) for c in "U(17zX(%,5.z^5(17l8(%,5.Z*(93-965$l7+-"])'
Jul 18 '05 #3
Pierre Quentel wrote:
Instead of indexing self.lab by strings, you can index them by the
attributes themselves : self.lab[self.i], and change line 23 into

for var in (self.s, self,i)

For your example, I wouldn't have used the "text" option in the
definition of the labels, then "textvariable" in the callback method
(procedi) ; I would have used only "text" and no IntVar or StringVar,
just an integer and a string :

class MiaApp:
def __init__(self, genitore):
self.mioGenitore = genitore
self.i = 42
self.s = "Baobab"
self.lab = {}
self.lab[self.i] = Label(self.mioGenitore)
self.lab[self.i].configure(width = 30, relief = RIDGE,
text = "[vuota]")
self.lab[self.i].pack()
self.lab[self.s] = Label(self.mioGenitore)
self.lab[self.s].configure(width = 30, relief = RIDGE,
text = "[vuota]")
self.lab[self.s].pack()
self.but = Button(self.mioGenitore)
self.but.configure(text = "Vai!", command = self.procedi)
self.but.pack()
def procedi(self):
for var in (self.i, self.s):
self.lab[var].configure(text = var)

Regards,
Pierre


hi pierre.

i don't think this would not have worked as expected (by me). in my
intentions, the text of the label must be slaved to a variable, so that
it would change dynamically during the mainloop execution if another
part of the code had chenged the content of the variable.

maybe here is a more convincing example (the previous one was contrived
too hastily i guess):

1 from Tkinter import *
2
3 class MiaApp:
4 def __init__(self, genitore):
5 self.mioGenitore = genitore
6 self.var = {0: 42, 1: "Baobab"}
7 self.lab = {}
8 self.lab[0] = Label(self.mioGenitore)
9 self.lab[0].configure(width = 30, relief = RIDGE,
10 text = "[vuota]")
11 self.lab[0].pack()
12 self.lab[1] = Label(self.mioGenitore)
13 self.lab[1].configure(width = 30, relief = RIDGE,
14 text = "[vuota]")
15 self.lab[1].pack()
16 self.but = Button(self.mioGenitore)
17 self.but.configure(text = "Vai!", command = self.procedi)
18 self.but.pack()
19 self.but2 = Button(self.mioGenitore)
20 self.but2.configure(text = "Torna!", command =
self.procedi2)
21 self.but2.pack()
22 def procedi(self):
23 for var in self.lab.keys():
24 self.lab[var].configure(text = self.var[var])
25 def procedi2(self):
26 self.var[0] = 24
27 self.var[1] = "Cactus"
28
29 radice = Tk()
30 miaApp = MiaApp(radice)
31 radice.mainloop()

in this example, when user presses "Torna!", the labels are not updated
as i expect; they only will be when user presses "Vai!" again (not what
i want).

thanks again

macs
Jul 18 '05 #4
Eric Brunel wrote:
On Tue, 29 Mar 2005 22:32:59 +0200, Pierre Quentel
<qu************@wanadoo.fr> wrote:
[...]


mr brunel,

i thank you for prompt reply. i will take my time to read it carefully.

meanwhile, i inform you and the ng that someone else gave me a quick and
dirty answer to my problem, namely subststuting line #24 like this:

24 self.lab[var].configure(textvariable = eval(var))

which seems to work as desired.

thanks again

bye

macs
Jul 18 '05 #5
Eric Brunel wrote:
On Tue, 29 Mar 2005 22:32:59 +0200, Pierre Quentel
<qu************@wanadoo.fr> wrote:
Instead of indexing self.lab by strings, you can index them by the
attributes themselves : self.lab[self.i], and change line 23 into

for var in (self.s, self,i)

I really think this is asking for trouble: I suppose that the i and s
attributes are meant to change at some point in the future, and you're
mapping their *values* to the corresponding labels. So if the value
changes, you won't be able to get the label again.
For your example, I wouldn't have used the "text" option in the
definition of the labels, then "textvariable" in the callback method
(procedi) ; I would have used only "text" and no IntVar or StringVar,
just an integer and a string :

I would have done exactly the contrary, as it is far more easier to use.
Using the textvariable option in Label's does not require you to
remember the labels, but only the variables used to hold their contents.
I'd also use two mappings: the first mapping a name to a Tkinter
variable for the label variables, and the second for the values to give
to these variables later.

Here is my version of the class code:

class MiaApp:
def __init__(self, genitore):
self.mioGenitore = genitore
## Mapping for variables
self.variables = {
"i" : StringVar(),
"s" : StringVar()
}
## Mapping for future variable values
self.values = {
"i" : 42,
"s" : "Baobab"
}
## Now, create the labels
for var in self.variables.values():
## Default text
var.set("[vuota]")
## Create label
lb = Label(self.mioGenitore, width=30, relief=RIDGE,
textvariable=var)
lb.pack()
## The button is no more remembered in an attribute, as it does
not seem to be needed
but = Button(self.mioGenitore, text = "Vai!", command =
self.procedi)
but.pack()

def procedi(self):
## Just change the variable values
for varName in self.variables.keys():
self.variables[varName].set(self.values[varName])


your technique is most interirting and clean, i must say.

nevertheless, i cannot find a natural way to "modularize" it, in such a
way that the machinery of the method might be isolated from the code
that uses it.

consider for example the following two programs:

.....

$ cat Miodialogo.py
from Tkinter import *

class MioDialogo(Toplevel):
def __init__(self, genitore, chiamante):
Toplevel.__init__(self, genitore)
self.wm_title("Valori delle variabili")

self.mioGenitore = genitore
self.mioChiamante = chiamante

self.fonteVar = ("Helvetica", 14)

self.quadro_grande = Frame(self)
self.quadro_grande.pack(expand = YES, fill = BOTH)

self.titolo = Label(self.quadro_grande)
self.titolo.configure(
text = "Valori delle variabili:",
width = 20,
font = self.fonteVar
)
self.titolo.pack(side = TOP, fill = X)

def mostraVariabili(self, *argomenti):
lung = 1
for i in argomenti:
if len(i) > lung:
lung = len(i)

self.dq = {}
self.dn = {}
self.dv = {}
for i in argomenti:
self.dq[i] = Frame(self.quadro_grande)
self.dq[i].pack(
side = TOP,
anchor = W,
fill = X
)

self.dn[i] = Label(self.dq[i])
self.dn[i].configure(
text = i + ": ",
width = lung + 2,
anchor = W
)
self.dn[i].pack(
side = LEFT
)

self.dv[i] = Label(self.dq[i])
self.dv[i].configure(
textvariable = eval("self.mioChiamante." + i),
anchor = W
)
self.dv[i].pack(
side = LEFT,
expand = YES,
fill = X
)

self.vaBene = Button(self.quadro_grande)
self.vaBene.configure(
text = "Va Bene",
command = self.pulsanteVaBenePremuto,
default = ACTIVE
)
self.vaBene.bind(
"<Return>",
self.pulsanteVaBenePremuto_a
)
self.vaBene.focus_force()
self.vaBene.pack(
side = BOTTOM,
pady = 2
)

def pulsanteVaBenePremuto(self):
self.destroy()
self.mioChiamante.var.configure(state = NORMAL)

def pulsanteVaBenePremuto_a(self, evento):
self.pulsanteVaBenePremuto()

$ cat spunta-4.py
from Tkinter import *
from Miodialogo import *

class MiaApp:
def __init__(self, genitore):

self.mioGenitore = genitore

self.fonte = ("Helvetica", 12)

self.quadro_grande = Frame(genitore)
self.quadro_grande.pack(expand = YES, fill = BOTH)

self.msg = Label(self.quadro_grande)
self.msg.configure(
font = self.fonte,
wraplength = "10c",
justify = LEFT,
text = u"Sono qui sotto presentati tre pulsanti a spunta. \
Premendo un pulsante, se ne varia lo stato di selezione e si \
imposta una variabile a un valore che indica lo stato del \
pulsante stesso. Premendo il pulsante \u00ABMostra \
Variabili\u00BB si possono vedere i valori correnti delle \
variabili."
)
self.msg.pack(side = TOP)

self.pulsanti = Frame(self.quadro_grande)
self.pulsanti.pack(side = BOTTOM, fill = X, padx = "2m")

self.pulsanti_spunta = Frame(self.quadro_grande)
self.pulsanti_spunta.pack(side = TOP, fill = X, padx = "2m")

self.annulla = Button(self.pulsanti)
self.annulla.configure(
text = "Annulla",
command = self.mioGenitore.destroy
)
self.annulla.pack(side = LEFT, expand = YES)
self.var = Button(self.pulsanti)
self.var.configure(
text = "Mostra Variabili",
command = self.pulsanteMostraVariabiliPremuto,
default = NORMAL
)
self.var.pack(side = LEFT, expand = YES)

self.tergicristalli = IntVar()
self.b1 = Checkbutton(self.pulsanti_spunta)
self.b1.configure(
text = "Tergicristalli a posto",
variable = self.tergicristalli,
relief = FLAT
)
self.b1.pack(
side = TOP,
pady = 2,
anchor = W
)

self.freni = IntVar()
self.b2 = Checkbutton(self.pulsanti_spunta)
self.b2.configure(
text = "Freni a posto",
variable = self.freni,
relief = FLAT
)
self.b2.pack(
side = TOP,
pady = 2,
anchor = W
)

self.autista = IntVar()
self.b3 = Checkbutton(self.pulsanti_spunta)
self.b3.configure(
text = "Autista sobrio",
variable = self.autista,
relief = FLAT
)
self.b3.pack(
side = TOP,
pady = 2,
anchor = W
)

def pulsanteMostraVariabiliPremuto(self):
if self.var.cget("state") == ACTIVE:
self.var.configure(state = DISABLED)
self.dialogo = MioDialogo(self.mioGenitore, self)
self.dialogo.mostraVariabili("tergicristalli",
"freni",
"autista")

radice = Tk()
radice.wm_title("Dimostrazione Pulsanti a Spunta")
radice.wm_iconname("spunta")
miaApp = MiaApp(radice)
radice.mainloop()

.....

the program spunta-4.py uses the module Miodialogo.py, which can be
usedby any other program simply passing along strings containing the
names of the attributes to be displayed.

i cannot find a similar way to modularize your technique (maybe it is my
fault).

thanks again

macs
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ivan Letal | last post by:
I have just tried this code.. Tkinter import * root = Tk() def callback(event): print "clicked at", event.x, event.y frame = Frame(root, width=100, height=100)
2
by: Irmen de Jong | last post by:
Hi, I'm having trouble with the code below. It's just a regular Tk text widget in which you can type and select text as expected, however the call to tkFileDialog.askopenfilename() seems to screw...
2
by: Russell E. Owen | last post by:
I want to support execution of simple user-written scripts in a Tkinter application. The scripts should be able to wait for data and such without hanging the GUI (and without having to write the...
2
by: Martin Jensen | last post by:
Hi I have a problem with Qt. My class definition is this: class Button : public QObject, public Tk_Object { Q_OBJECT public: Button() {} Button(Tk_Object &p); ~Button();
0
by: syed_saqib_ali | last post by:
Below is a simple code snippet showing a Tkinter Window bearing a canvas and 2 connected scrollbars (Vertical & Horizontal). Works fine. When you shrink/resize the window the scrollbars adjust...
2
by: Stewart Midwinter | last post by:
this has me puzzled; I've created a small test app to show the problem I'm having. I want to use subprocess to execute system commands from inside a Tkinter app running under Cygwin. When I...
0
by: Stewart Midwinter | last post by:
I have a Tkinter app running on cygwin. It includes a Test menu item that does nothing more than fetch a directory listing and display it in a Toplevel window (I'd use a tkMessageBox showinfo...
2
by: Andrew Trevorrow | last post by:
Our app uses embedded Python to allow users to run arbitrary scripts. Scripts that import Tkinter run fine on Windows, but on Mac OS X there is a serious problem. After a script does "root = Tk()"...
3
by: H J van Rooyen | last post by:
Hi, Still struggling with my GUI exercise - I have the following lines of code in a routine that is bound at <Key-Returnto an instance of Entry : self.disp.Amount_des = Label(self.disp,...
4
by: Davy | last post by:
Hi all, I have written a simple Tkinter program, that is draw a rectangle in a canvas, when I press Up key, the rectangle move up. But the program seems work not properly? My environment is...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.