[Tutor] I get an error while search in my Entry field

2018-06-18 Thread Ali M
Hi, i get an error while searching my Entry field and i don't understand
what that means.

code:

#! /usr/bin/env python3
#GeologyDict by Ali M
import sqlite3 as sqlite
import tkinter as tk
from tkinter import Text
from tkinter import Entry
from tkinter import Scrollbar
from tkinter import ttk

#GUI Widgets


class EsperantoDict:
def __init__(self, master):

master.title("EsperantoDict")
master.resizable(False, False)
master.configure(background='#EAFFCD')

self.search_var = tk.StringVar()
self.search_var.trace("w", lambda name, index, mode:
self.update_list())

self.style = ttk.Style()
self.style.configure("TFrame", background='#EAFFCD')
self.style.configure("TButton", background='#EAFFCD')
self.style.configure("TLabel", background='#EAFFCD')

self.frame_header = ttk.Frame(master, relief=tk.FLAT)
self.frame_header.pack(side=tk.TOP, padx=5, pady=5)

self.logo = tk.PhotoImage(file=r'C:\EsperantoDict\eo.png')
self.small_logo = self.logo.subsample(10, 10)

ttk.Label(self.frame_header, image=self.small_logo).grid(row=0,
column=0, stick="ne", padx=5, pady=5, rowspan=2)
ttk.Label(self.frame_header, text='EsperantoDict', font=('Arial',
18, 'bold')).grid(row=0, column=1)

self.frame_content = ttk.Frame(master)
self.frame_content.pack()

self.entry_search = ttk.Entry(self.frame_content,
textvariable=self.search_var)
self.entry_search.grid(row=0, column=0)
self.entry_search.bind('', self.entry_delete)

self.button_search = ttk.Button(self.frame_content, text="Search")
self.aks = tk.PhotoImage(file=r'C:\EsperantoDict\search.png')
self.small_aks = self.aks.subsample(3, 3)
self.button_search.config(image=self.small_aks, compound=tk.LEFT)
self.button_search.grid(row=0, column=1, columnspan=2)

self.listbox = tk.Listbox(self.frame_content, height=28)
self.listbox.grid(row=1, column=0)
self.scrollbar = ttk.Scrollbar(self.frame_content,
orient=tk.VERTICAL, command=self.listbox.yview)
self.scrollbar.grid(row=1, column=1, sticky='ns')
self.listbox.config(yscrollcommand=self.scrollbar.set)
self.listbox.bind('<>', self.enter_meaning)

self.textbox = tk.Text(self.frame_content, width=60, height=27)
self.textbox.grid(row=1, column=2)

# SQLite
self.db = sqlite.connect(r'C:\EsperantoDict\test.db')
self.cur = self.db.cursor()
self.cur.execute('SELECT Esperanto FROM Words')
for row in self.cur:
self.listbox.insert(tk.END, row)
# SQLite

def enter_meaning(self, tag):
if self.listbox.curselection():
results = self.cur.execute("SELECT English FROM Words WHERE
rowID = 1")
for row in results:
self.textbox.insert(tk.END, row)

def update_list(self):
search_term = self.search_var.get()
self.listbox.delete(0, tk.END)
for item in self.listbox:
if search_term.lower() in item.lower():
self.listbox.insert(tk.END, item)

def entry_delete(self, tag):
self.entry_search.delete(0, tk.END)
return None


def main():
root = tk.Tk()
esperantodict = EsperantoDict(root)
root.mainloop()

if __name__ == '__main__': main()

#db tbl name: Words
##db first field name: Esperanto
##db second field name: English

error:
Exception in Tkinter callback
Traceback (most recent call last):
  File
"C:\Users\deadmarshal\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py",
line 1702, in __call__
return self.func(*args)
  File "C:/EsperantoDict/EsperantoDict.py", line 21, in 
self.search_var.trace("w", lambda name, index, mode: self.update_list())
  File "C:/EsperantoDict/EsperantoDict.py", line 77, in update_list
for item in self.listbox:
  File
"C:\Users\deadmarshal\AppData\Local\Programs\Python\Python36\lib\tkinter\__init__.py",
line 1486, in cget
return self.tk.call(self._w, 'cget', '-' + key)
TypeError: must be str, not int
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] What's the difference between calling a method with parenthesis vs. without it?

2018-06-18 Thread C W
Dear Python experts,

I never figured out when to call a method with parenthesis, when is it not?
It seems inconsistent.

For example,
If I do

> data.isnull()

numberair_pressure_9amair_temp_9amavg_wind_direction_9amavg_wind_speed_9am
max_wind_direction_9ammax_wind_speed_9amrain_accumulation_9am
rain_duration_9amrelative_humidity_9amrelative_humidity_3pm
0 False False False False False False False False False False False
1 False False False False False False False False False False False
2 False False False False False False False False False False False

Or I can do without parenthesis,
> data.isnull

 data.columns

Index(['number', 'air_pressure_9am', 'air_temp_9am', 'avg_wind_direction_9am',
   'avg_wind_speed_9am', 'max_wind_direction_9am', 'max_wind_speed_9am',
   'rain_accumulation_9am', 'rain_duration_9am', 'relative_humidity_9am',
   'relative_humidity_3pm'],
  dtype='object')


# with parenthesis throws an error
> data.columns()

---TypeError
Traceback (most recent call
last) in ()> 1
data.columns()
TypeError: 'Index' object is not callable


I always thought columns(), isnull() are methods to objects, so, you would
need to pass parameters even when it's empty. But, apparently I am wrong on
this.

What is going on?

Thank you very much!
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] I get an error while search in my Entry field

2018-06-18 Thread Alan Gauld via Tutor
On 17/06/18 23:59, Ali M wrote:

> def update_list(self):
> search_term = self.search_var.get()
> self.listbox.delete(0, tk.END)
> for item in self.listbox:

The above pair of lines look odd.
You delete everything on the listbox then try to iterate
over it? Is that really what you want?

Also are you sure you can iterate over a listbox widget?
I was not aware of that capability and can see nothing
about iterators in the listbox help screen...

I usually use the get() method to fetch the contents
before trying to iterate over them:

for item in myList.get(0,tk.END):
   # use item

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] What's the difference between calling a method with parenthesis vs. without it?

2018-06-18 Thread Alan Gauld via Tutor
On 17/06/18 19:02, C W wrote:
> I never figured out when to call a method with parenthesis, when is it not?

You call anything in Python (function, method, class, etc) using
parentheses. The parentheses are what makes it a call.

When you don't use parentheses you are referencing the
callable object.

>> data.isnull()

That calls the isnull method of data

>> data.isnull

That references the isnull method, so you could store it in a variable
for future use:

checkNull = data.isnull
# do other stuff.

if checkNull():  # call data.isnull() via the variable
   # do more stuff

>  air_temp_9am  avg_wind_direction_9am  \
> 0  0918.06 74.822000  271.10

But I've no idea why you get this output...

> Obviously, the second case does not make any senses. 

The fact that its a method makes sense, the "data" makes no sense.


> But, in data.columns,
> it is only correct to do without parenthesis as below:
> 
>> data.columns
> 
> Index(['number', 'air_pressure_9am', 'air_temp_9am', 'avg_wind_direction_9am',

So data.columns is not a method but an attribute.
The attribute apparently references an Index object.

> # with parenthesis throws an error
>> data.columns()

We've already established that data.columns is a reference
to an Index object and you can't call an Index object,
it is not "callable"

Traceback (most recent call
> last) in ()> 1
> data.columns()
> TypeError: 'Index' object is not callable

Which is what Python is telling you.

> I always thought columns(), isnull() are methods to objects, 

isnull is apparenmtly a method.
columns is apparenmtly an attribute.

They are different things and you need to read the
documentation (or code) to see which is which.
You can call a method.
You can only call an attribute if it stores a callable object.

And if you use a method name without parens you
get a reference to the method back.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] What's the difference between calling a method with parenthesis vs. without it?

2018-06-18 Thread Peter Otten
C W wrote:

> Dear Python experts,
> 
> I never figured out when to call a method with parenthesis, when is it
> not? It seems inconsistent.
> 
> For example,
> If I do
> 
>> data.isnull()
> 
> numberair_pressure_9amair_temp_9amavg_wind_direction_9amavg_wind_speed_9am
> max_wind_direction_9ammax_wind_speed_9amrain_accumulation_9am
> rain_duration_9amrelative_humidity_9amrelative_humidity_3pm
> 0 False False False False False False False False False False False
> 1 False False False False False False False False False False False
> 2 False False False False False False False False False False False
> 
> Or I can do without parenthesis,
>> data.isnull
> 
>  air_temp_9am  avg_wind_direction_9am  \
> 0  0918.06 74.822000  271.10
> 1  1917.347688 71.403843  101.935179
> 2  2923.04 60.638000   51.00
> 
> 
> Obviously, the second case does not make any senses. 

Let's try with our own class:

>>> class A:
... def foo(self): return "bar"
... 
>>> a = A()
>>> a.foo()
'bar'

With parens the foo method is executed and returns the expected result whose 
repr() is then printed by the interactive interpreter.

>>> a.foo
>

Without parens the repr() of the method is printed just like the repr of any 
other value would be.

That repr() has the form 



and in your case REPR_OF_INSTANCE happens to be a lengthy dump of the 
DataFrame. To verify our findings let's define a custom __repr__ for out A:

>>> class A:
... def foo(self): return "bar"
... def __repr__(self): return "yadda " * 10
... 
>>> A().foo




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


Re: [Tutor] I get an error while search in my Entry field

2018-06-18 Thread Peter Otten
Alan Gauld via Tutor wrote:

> On 17/06/18 23:59, Ali M wrote:
> 
>> def update_list(self):
>> search_term = self.search_var.get()
>> self.listbox.delete(0, tk.END)
>> for item in self.listbox:
> 
> The above pair of lines look odd.
> You delete everything on the listbox then try to iterate
> over it? Is that really what you want?

More likely the words have to be reread from the database.
  
> Also are you sure you can iterate over a listbox widget?
> I was not aware of that capability and can see nothing
> about iterators in the listbox help screen...

You cannot iterate over Listbox widgets, it's just that for every object 
with a __getitem__() method Python will try and feed it consecutive integers

>>> class A:
... def __getitem__(self, index):
... if index > 3: raise IndexError
... return index * index
... 
>>> list(A())
[0, 1, 4, 9]

a feature that probably predates the iterator protocol.
However, the tkinter widgets expect option names like "background", or 
"borderstyle", and respond with the somewhat cryptic TypeError when the 0 is 
passed instead.
 
> I usually use the get() method to fetch the contents
> before trying to iterate over them:
> 
> for item in myList.get(0,tk.END):
># use item
> 


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


Re: [Tutor] What's the difference between calling a method with parenthesis vs. without it?

2018-06-18 Thread Steven D'Aprano
On Sun, Jun 17, 2018 at 02:02:07PM -0400, C W wrote:
> Dear Python experts,
> 
> I never figured out when to call a method with parenthesis, when is it not?
> It seems inconsistent.

You *always* CALL a method (or function) with parentheses.

But sometimes you can grab hold of a method (or function) *without* 
calling it, by referring to its name without parentheses.

The trick is to understand that in Python, functions and methods are 
what they call "First-class citizens" or first-class values, just like 
numbers, strings, lists etc.

https://en.wikipedia.org/wiki/First-class_citizen

Let's take a simple example:

def add_one(x=0):
return x + 1


To CALL the function, it always needs parentheses (even if it doesn't 
require an argument):

py> add_one(10)
11
py> add_one()
1

But to merely refer to the function by name, you don't use parentheses. 
Then you can treat the function as any other value, and print it, assign 
it to a variable, pass it to a different function, or stick it in a 
list. Anything you can do with anything else, really.

For example:

py> myfunction = add_one  # assign to a variable
py> print(myfunction)

py> L = [1, "a", add_one, None]  # put it inside a list
py> print(L)
[1, 'a', , None]


If it isn't clear to you why anyone would want to do this in the first 
place, don't worry too much about it, it is a moderately advanced 
technique. But it is essential for such things as:

- function introspection;
- callback functions used in GUI programming;
- functional programming;

etc. For example, you may have seen the map() function. Here it is in 
action:

py> list(map(add_one, [1, 10, 100, 1000]))
[2, 11, 101, 1001]


Can you work out what it does? If not, try opening the interactive 
interpreter, and enter:

help(map)

and see if that helps.

Or ask here :-)



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


Re: [Tutor] What's the difference between calling a method with parenthesis vs. without it?

2018-06-18 Thread Steven D'Aprano
Another thought comes to mind...

On Sun, Jun 17, 2018 at 02:02:07PM -0400, C W wrote:

> Obviously, the second case does not make any senses. But, in data.columns,
> it is only correct to do without parenthesis as below:
> 
> > data.columns
[...]
> # with parenthesis throws an error
> > data.columns()


That's because data.columns isn't a method. Since it isn't a method, it 
is just a regular attribute (or possibly a read-only attribute) that you 
can retrieve the contents, but not call.

Some languages call these "instance variables". I don't like that term, 
but in this case the analogy with variables is good. Consider two named 
objects, one which is a function, one which is not:

py> len

py> len("hello world")  # try calling len
11

py> length = 99
py> length()  # try calling length
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'int' object is not callable


The reason for this should be fairly obvious, but I trust you can see 
that *in general* there is no fool-proof way of knowing which names 
refer to callable functions and which refer to non-callable values. You 
have to know the context of the code, read the documentation or manual, 
or just try it and see what happens.

The same can apply to methods and other attributes:

class MyClass:

length = 99

def len(self, object):
# Re-invent the wheel, badly.
count = 0
for item in object:
count += 1
return count



And now to see the same behaviour as for ordinary variables:


py> instance = MyClass()
py> instance.length
99
py> instance.length()
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'int' object is not callable

py> print(instance.len)
>
py> instance.len("hello world")
11


Does this help you understand what you are seeing with the data.columns 
example?


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


Re: [Tutor] What's the difference between calling a method with parenthesis vs. without it?

2018-06-18 Thread Mats Wichmann
On 06/17/2018 12:02 PM, C W wrote:
> Dear Python experts,
> 
> I never figured out when to call a method with parenthesis, when is it not?
> It seems inconsistent.
> 
> For example,
> If I do
> 
>> data.isnull()
> 
> numberair_pressure_9amair_temp_9amavg_wind_direction_9amavg_wind_speed_9am
> max_wind_direction_9ammax_wind_speed_9amrain_accumulation_9am
> rain_duration_9amrelative_humidity_9amrelative_humidity_3pm
> 0 False False False False False False False False False False False
> 1 False False False False False False False False False False False
> 2 False False False False False False False False False False False
> 
> Or I can do without parenthesis,
>> data.isnull
> 
>  air_temp_9am  avg_wind_direction_9am  \
> 0  0918.06 74.822000  271.10
> 1  1917.347688 71.403843  101.935179
> 2  2923.04 60.638000   51.00
> 
> 
> Obviously, the second case does not make any senses. But, in data.columns,
> it is only correct to do without parenthesis as below:
> 
>> data.columns
> 
> Index(['number', 'air_pressure_9am', 'air_temp_9am', 'avg_wind_direction_9am',
>'avg_wind_speed_9am', 'max_wind_direction_9am', 'max_wind_speed_9am',
>'rain_accumulation_9am', 'rain_duration_9am', 'relative_humidity_9am',
>'relative_humidity_3pm'],
>   dtype='object')
> 
> 
> # with parenthesis throws an error
>> data.columns()

I think you've already got all you need, let me just try one more brief
summary, in case it helps - Python is not like certain other languages.

1. a def statement is a runnable statement that creates an object (class
statements are also runnable and also create an object)
2. you can only call objects that are callable (you have already seen
the error you get if you try)
3. you call by using the () syntax
4. you can refer to an object by a name bound to it without calling it.
IF you do so in a context that implies you are "printing" it, Python
will invoke a special method (called __repr__) to attempt to create a
human-readable representation of the object.  I only mention that
because what it prints out may not be what you expect - but you should
know that what is printing is a transformation of object -> helpful
string.  Try some experiments with this to get comfortable.







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


[Tutor] Recursion

2018-06-18 Thread Roger Lea Scherer
My foggy understanding of recursion is probably the reason I can't figure
this out. When turtle draws this program there is an orange line in the
green which I would prefer not to have. I've tried all I could think of,
but can't get the orange line to go away, or maybe more accurately, not to
be drawn.

The program goes to the end of the recursion and then starts drawing? which
seems wrong, because the trunk of the tree is drawn first. Maybe: How does
it know to draw the second orange line? and how does it know when to stop
with only two branches from the trunk?

Thank you as always.
-
import turtle

def tree(branchLen, width, t):
if branchLen > 5:
t.pensize(width)
t.forward(branchLen)
t.right(20)
tree(branchLen-15, width-5, t)
t.left(40)
tree(branchLen-15, width-5, t)
t.pencolor("orange")
t.right(20)
t.backward(branchLen)
t.pencolor("green")

def main():
t = turtle.Turtle()
myWin = turtle.Screen()
t.speed(0)
t.left(90)
t.up()
t.backward(100)
t.down()
t.color("green")
tree(75, 20, t)
myWin.exitonclick()

main()

-- 
​
Roger Lea Scherer
623.255.7719

  *Strengths:*
   Input, Strategic,
​ ​
Responsibility,
​​
Learner, Ideation
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Recursion

2018-06-18 Thread Alan Gauld via Tutor
On 18/06/18 23:12, Roger Lea Scherer wrote:
> My foggy understanding of recursion is probably the reason I can't figure
> this out. When turtle draws this program there is an orange line in the
> green which I would prefer not to have. I've tried all I could think of,
> but can't get the orange line to go away, or maybe more accurately, not to
> be drawn.

Remove the line in tree() that sets the color to orange?

> The program goes to the end of the recursion and then starts drawing? which
> seems wrong, because the trunk of the tree is drawn first.

You call the recursing function before and after drawing
so it draws on the last call and on the way back up to
the first call, like this:

tree
   draw
   tree
 draw
 tree
draw
tree
draw
 draw
   draw
draw

> it know to draw the second orange line? 

I'm not sure what you mean by second but you draw orange
in every call to tree.

> and how does it know when to stop
> with only two branches from the trunk?

Again I'm not sure what's happening because I'm not
running the code.

> def tree(branchLen, width, t):
> if branchLen > 5:
> t.pensize(width)
> t.forward(branchLen)

This should draw in green

> t.right(20)
> tree(branchLen-15, width-5, t)

this draws in green and orange

> t.left(40)
> tree(branchLen-15, width-5, t)

this draws green and orange

> t.pencolor("orange")
> t.right(20)
> t.backward(branchLen)

this draws orange

> t.pencolor("green")


HTH
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


Re: [Tutor] What's the difference between calling a method with parenthesis vs. without it?

2018-06-18 Thread Steven D'Aprano
On Mon, Jun 18, 2018 at 08:50:24AM -0600, Mats Wichmann wrote:

> Python is not like certain other languages.

But it is like *other* certain other languages.

:-)

Python's execution model is different from C, Pascal or Fortran, but it 
is quite similar to Ruby, Lua and Javascript.



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