Re: [Tutor] Help with class in class

2012-09-09 Thread Dave Angel
On 09/09/2012 04:56 PM, leam hall wrote:
> self.blue = Button(root, text="Blue",
> command=self.change_text_color("blue"))
> self.blue.pack(side=LEFT)
> self.green = Button(root, text="Green",
> command=self.change_text_color("green"))
> self.green.pack(side=LEFT)
>
> To follow up, I've added a second button. Of course, it doesn't work, the
> text comes out as the color of the last button. It is running the commands
> in the Button line without the buttons being clicked.
>
> More research...
>
>

Please reread Peter's message, starting with "Another problem..."

He identifies the problem, and mentions one way to fix it.  In your
response, you mentioned to lambda, so you must have seen it.

In particular, the Button() function takes a function object as its
command= parameter.  You call the function yourself, and pass the return
value, which of course is not what's wanted.  You don't want the
function called till someone presses the button.  So you want something
equivalent to
  = Button(root, text="Blue", command=change_to_green)

Notice that there are NO parentheses on that change_to_green.  You want
the function object, you do NOT want to call the function.



-- 

DaveA

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with class in class

2012-09-09 Thread leam hall
self.blue = Button(root, text="Blue",
command=self.change_text_color("blue"))
self.blue.pack(side=LEFT)
self.green = Button(root, text="Green",
command=self.change_text_color("green"))
self.green.pack(side=LEFT)

To follow up, I've added a second button. Of course, it doesn't work, the
text comes out as the color of the last button. It is running the commands
in the Button line without the buttons being clicked.

More research...


-- 
Mind on a Mission 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with class in class

2012-09-09 Thread leam hall
On Sun, Sep 9, 2012 at 12:12 PM, Peter Otten <__pete...@web.de> wrote:

> the above will no longer complain about a missing attribute.
>
> > root = Tk()
> > project = ch8_Project(master=root)
> > project.mainloop()
>
>
> Another problem that caught my attention:
>
> > self.green = Button(root, text="Green",
> command=self.change_text_color("green"))
>
>
> The command argument is supposed to be a function; you are instead
> assigning
> the result of a method call (which is None in this case, but as a side
> effect will set the color to green immediately. The simplest fix is to
> define a helper function that takes no arguments
>  ...
>  def change_to_green():
>  self.change_text_color("green")
>  self.green = Button(root, text="Green", command=change_to_green)
>  ...
>
> If you already know about lambda you can try to rewrite this using that.
>
>
Peter, thank you for helping me understand the scope issue I was missing! I
knew it was something like that but forget that omitting "this." meant the
variables were limited in scope to that method.

I have seen lamba but do not understand it. The project requires four
buttons, each one turns the same text a different color. The class is also
Python 3 based.  :)

Leam



-- 
Mind on a Mission 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with class in class

2012-09-09 Thread Peter Otten
leam hall wrote:

> I'm in the O'Reilly Python 2 class, so pointers to learning would be
> better than just answers, please. My brain is a bit slow but needs to go
> forward.
> 
> Line 16 calls line 31. Rather, it is supposed to. I'm trying to figure out
> why I get
> 
>   File "./ch8_project.py", line 31, in change_text_color
> self.text.tag_configure('highlightline', foreground=fg_color)
> AttributeError: 'ch8_Project' object has no attribute 'text'
> 
> If line 31 starts with "self." and
> 
>   File "./ch8_project.py", line 31, in change_text_color
> text.tag_configure('highlightline', foreground=fg_color)
> NameError: global name 'text' is not defined
> 
> 
> If Line 31 does not start with "self.". I understand the second problem
> but not the first.
> 
> What am I missing?

When inside a method you assign to

self.whatever = ...

you are setting an attribute of the class. If you assign to a name

whatever = ...

you are setting a local variable. So

> #!/usr/bin/env python3
> 
> from tkinter import *
> 
> SIDE = W+E 
> ALL = N+E+W+S
> 
> class ch8_Project(Frame):
> 
>   def __init__(self, master=None):
> Frame.__init__(self, master)
> self.file_open("./fred")
> fg_color = "Red"
> self.pack()
> self.add_text_widget( fg_color, self.out_text)
> self.green = Button(root, text="Green", 
command=self.change_text_color("green"))
> self.green.pack(side=TOP)
> 
>   def add_text_widget(self, fg_color, out_text):
> text = Text(root)

in the above line you are making a local variable which will be forgotten 
when the method terminates. Change the line to

  self.text = Text(root)

do the same for the following lines, and

> text.insert(INSERT, out_text)
> text.tag_configure('highlightline', foreground='red')
> text.tag_add('highlightline', '1.0', 'end')
> text.pack()
> 
>   def file_open(self, file_name):
> file = open(file_name, "r")
> self.out_text = file.read()
> 
>   def change_text_color(self, fg_color):
> self.text.tag_configure('highlightline', foreground=fg_color)

the above will no longer complain about a missing attribute. 

> root = Tk()
> project = ch8_Project(master=root)
> project.mainloop()


Another problem that caught my attention:

> self.green = Button(root, text="Green", 
command=self.change_text_color("green"))


The command argument is supposed to be a function; you are instead assigning 
the result of a method call (which is None in this case, but as a side 
effect will set the color to green immediately. The simplest fix is to 
define a helper function that takes no arguments
 ...
 def change_to_green():
 self.change_text_color("green")
 self.green = Button(root, text="Green", command=change_to_green)
 ...

If you already know about lambda you can try to rewrite this using that.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with class in class

2012-09-09 Thread Kwpolska
On Sun, Sep 9, 2012 at 5:43 PM, leam hall  wrote:
> Of course, showing the code might help...
>
>  http://bpaste.net/show/44593/
>
> Thanks!
>
> Leam
>
>
> On Sun, Sep 9, 2012 at 10:42 AM, leam hall  wrote:
>>
>> I'm in the O'Reilly Python 2 class, so pointers to learning would be
>> better than just answers, please. My brain is a bit slow but needs to go
>> forward.
>>
>> Line 16 calls line 31. Rather, it is supposed to. I'm trying to figure out
>> why I get
>>
>>   File "./ch8_project.py", line 31, in change_text_color
>> self.text.tag_configure('highlightline', foreground=fg_color)
>> AttributeError: 'ch8_Project' object has no attribute 'text'
>>
>> If line 31 starts with "self." and
>>
>>   File "./ch8_project.py", line 31, in change_text_color
>> text.tag_configure('highlightline', foreground=fg_color)
>> NameError: global name 'text' is not defined
>>
>>
>> If Line 31 does not start with "self.". I understand the second problem
>> but not the first.
>>
>> What am I missing?
>>
>> Thanks!
>>
>> Leam
>>
>>
>> --
>> Mind on a Mission
>>
>
>
>
> --
> Mind on a Mission
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

Please do not top post.
---
And the problem is: add_text_widget() defines a local (function-wide)
`text` object rather than a class-wide one.  Just prepend the `text`
object with `self.` and it will be solved.  Also, if you are planning
to use more than one of those, you will need to do it in another way.
Bonus question: why py3k?

-- 
Kwpolska 
stop html mail  | always bottom-post
www.asciiribbon.org | www.netmeister.org/news/learn2quote.html
GPG KEY: 5EAAEA16
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help with class in class

2012-09-09 Thread leam hall
Of course, showing the code might help...

 http://bpaste.net/show/44593/

Thanks!

Leam

On Sun, Sep 9, 2012 at 10:42 AM, leam hall  wrote:

> I'm in the O'Reilly Python 2 class, so pointers to learning would be
> better than just answers, please. My brain is a bit slow but needs to go
> forward.
>
> Line 16 calls line 31. Rather, it is supposed to. I'm trying to figure out
> why I get
>
>   File "./ch8_project.py", line 31, in change_text_color
> self.text.tag_configure('highlightline', foreground=fg_color)
> AttributeError: 'ch8_Project' object has no attribute 'text'
>
> If line 31 starts with "self." and
>
>   File "./ch8_project.py", line 31, in change_text_color
> text.tag_configure('highlightline', foreground=fg_color)
> NameError: global name 'text' is not defined
>
>
> If Line 31 does not start with "self.". I understand the second problem
> but not the first.
>
> What am I missing?
>
> Thanks!
>
> Leam
>
>
> --
> Mind on a Mission 
>
>


-- 
Mind on a Mission 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Help with class in class

2012-09-09 Thread leam hall
I'm in the O'Reilly Python 2 class, so pointers to learning would be better
than just answers, please. My brain is a bit slow but needs to go forward.

Line 16 calls line 31. Rather, it is supposed to. I'm trying to figure out
why I get

  File "./ch8_project.py", line 31, in change_text_color
self.text.tag_configure('highlightline', foreground=fg_color)
AttributeError: 'ch8_Project' object has no attribute 'text'

If line 31 starts with "self." and

  File "./ch8_project.py", line 31, in change_text_color
text.tag_configure('highlightline', foreground=fg_color)
NameError: global name 'text' is not defined


If Line 31 does not start with "self.". I understand the second problem but
not the first.

What am I missing?

Thanks!

Leam


-- 
Mind on a Mission 
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor