[Tutor] Traffic Light

2013-01-19 Thread anthonym
Hello All,

I am new to Python and taking a course now.  One of my exercises is to
create a traffic light using tkinter.   The exercise also requires that I
create radio buttons that allow the user to click on the color and the
corresponding light will come on.

I followed some examples in my book to write the code but get some errors
that I don't understand .  Code and error messages below

Thanks,
Tony

from tkinter import *  # Import tkinter

class Trafficlight:
def __init__(self):
window = Tk()# Create a window
window.title("Traffic Light")   # Set a title

# Add three radio buttons to frame1
frame1 = Frame(window)   # Create and add a frame to window
frame1.pack()
self.v1 = IntVar()
self.v2 = IntVar()
rbRed = Radiobutton(frame1, text = "Red", bg = "red",
variable = self.v2,
value = 1,
command = self.processRadiobutton)
rbYellow = Radiobutton(frame1, text = "Yellow", bg = "yellow",
variable = self.v2, value = 2,
command = self.processRadiobutton)
rbGreen = Radiobutton(frame1, text = "Green", bg = "green",
variable = self.v2, value = 3,
command = self.processRadiobutton)

rbRed.grid(row = 10, column = 1)
rbYellow.grid(row = 10, column = 2)
rbGreen.grid(row = 10, column = 3)

# Add Radio Button process below once I figure that out

# Place canvas in the window

self.canvas = Canvas(window, width = 480, height = 480, bg =
"white")
self.canvas.pack()

# Display a rectangle

def displayRect(self):
self.canvas.create_rectangle(10, 10, 190, tages = "rect")

# Display a Oval for the top light

def displayOval(self):
self.canvas.create_oval(10, 10, 10, 10)

# Display an Oval for the middle light

def displayOval(self):
self.canvas.create_oval(20, 20, 20, 20)

# Display an Oval for the bottom light

def displayOval(self):
self.canvas.create_oval(30, 30, 30, 30)

 # Create an event loop

Trafficlight()



Error messages

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 02:56:36)
[GCC 4.2.1 (Apple Inc. build 5577)] on darwin
Type "copyright", "credits" or "license()" for more information.
>>> ==== RESTART 
>>> 
Traceback (most recent call last):
  File "/Users/anthonym/Downloads/pybook/Traffic Light Tony.py", line 57, in

Trafficlight()
  File "/Users/anthonym/Downloads/pybook/Traffic Light Tony.py", line 16, in
__init__
command = self.processRadiobutton)
AttributeError: 'Trafficlight' object has no attribute 'processRadiobutton'
>>>  RESTART ====
>>> 
Traceback (most recent call last):
  File "/Users/anthonym/Downloads/pybook/Traffic Light Tony.py", line 57, in

Trafficlight()
  File "/Users/anthonym/Downloads/pybook/Traffic Light Tony.py", line 16, in
__init__
command = self.processRadiobutton)
AttributeError: 'Trafficlight' object has no attribute 'processRadiobutton'
>>> 


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


Re: [Tutor] Traffic Light

2013-01-19 Thread anthonym
Thanks again for the info Alan.  I am still passing the button process
until I can get my rectangle and ovals on the canvass.  The program runs
and produces a window with the radio buttons on top.  I would like them on
the bottom but changes to the row field have no effect.

Anybody have any ideas on why I don't see my shape on the canvass?

Thanks,
Tony

On 1/19/13 1:02 PM, "Alan Gauld"  wrote:

>On 19/01/13 20:31, anthonym wrote:
>
>>  rbRed = Radiobutton(frame1, text = "Red", bg = "red",
>>  variable = self.v2,
>>  value = 1,
>>  command = self.processRadiobutton)
>
>Here you assign your method to the button
>
>>  # Add Radio Button process below once I figure that out
>
>But you haven't defined it, it does not exist.
>So python complains.
>
>You need to define something, even just a pass:
>
>
>  def processRadioButton(self): pass
>
>
>> 16, in __init__
>>  command = self.processRadiobutton)
>> AttributeError: 'Trafficlight' object has no attribute
>>'processRadiobutton'
>
>
>HTH
>
>-- 
>Alan G
>Author of the Learn to Program web site
>http://www.alan-g.me.uk/
>
>___
>Tutor maillist  -  Tutor@python.org
>To unsubscribe or change subscription options:
>http://mail.python.org/mailman/listinfo/tutor


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


Re: [Tutor] Traffic Light

2013-01-19 Thread anthonym
Sure thing.  Here is the code.  And after that is the box I get with the
radio buttons but no shapes.

from tkinter import *  # Import tkinter

class Trafficlight:
def __init__(self):
window = Tk()# Create a window
window.title("Traffic Light")   # Set a title

# Place canvas in the window

self.canvas = Canvas(window, width = 480, height = 480, bg =
"white")
self.canvas.pack()

# Add three radio buttons to frame1
frame1 = Frame(window)   # Create and add a frame to window
frame1.pack()
self.v1 = IntVar()
self.v2 = IntVar()
rbRed = Radiobutton(frame1, text = "Red", bg = "red",
variable = self.v2,
value = 1,
command = self.processRadiobutton)
rbYellow = Radiobutton(frame1, text = "Yellow", bg = "yellow",
variable = self.v2, value = 2,
command = self.processRadiobutton)
rbGreen = Radiobutton(frame1, text = "Green", bg = "green",
variable = self.v2, value = 3,
command = self.processRadiobutton)

rbRed.grid(row = 400, column = 1)
rbYellow.grid(row = 400, column = 2)
rbGreen.grid(row = 400, column = 3)

 
 
window.mainloop()# Create an event loop

# Display a rectangle

def displayRect(self):
self.canvas.create_rectangle(10, 10, 100, 100, tages = "rect")

# Display a Oval for the top light

def displayOval(self):
self.canvas.create_oval(10, 10, 10, 10)

# Display an Oval for the middle light

def displayOval(self):
self.canvas.create_oval(20, 20, 20, 20)

# Display an Oval for the bottom light

def displayOval(self):
self.canvas.create_oval(30, 30, 30, 30)



 
 
  

# Add Radio Button process below once I figure that out

def processRadiobutton(self): pass

Trafficlight()

from tkinter import *  # Import tkinter

class Trafficlight:
def __init__(self):
window = Tk()# Create a window
window.title("Traffic Light")   # Set a title

# Place canvas in the window

self.canvas = Canvas(window, width = 480, height = 480, bg =
"white")
self.canvas.pack()

# Add three radio buttons to frame1
frame1 = Frame(window)   # Create and add a frame to window
frame1.pack()
self.v1 = IntVar()
self.v2 = IntVar()
rbRed = Radiobutton(frame1, text = "Red", bg = "red",
variable = self.v2,
value = 1,
command = self.processRadiobutton)
rbYellow = Radiobutton(frame1, text = "Yellow", bg = "yellow",
variable = self.v2, value = 2,
command = self.processRadiobutton)
rbGreen = Radiobutton(frame1, text = "Green", bg = "green",
variable = self.v2, value = 3,
command = self.processRadiobutton)

rbRed.grid(row = 400, column = 1)
rbYellow.grid(row = 400, column = 2)
rbGreen.grid(row = 400, column = 3)

 
 
window.mainloop()# Create an event loop

# Display a rectangle

def displayRect(self):
self.canvas.create_rectangle(10, 10, 100, 100, tages = "rect")

# Display a Oval for the top light

def displayOval(self):
self.canvas.create_oval(10, 10, 10, 10)

# Display an Oval for the middle light

def displayOval(self):
self.canvas.create_oval(20, 20, 20, 20)

# Display an Oval for the bottom light

def displayOval(self):
self.canvas.create_oval(30, 30, 30, 30)



 
 
  

# Add Radio Button process below once I figure that out

def processRadiobutton(self): pass

Trafficlight()













On 1/19/13 3:14 PM, "Matthew Ngaha"  wrote:

>On Sat, Jan 19, 2013 at 10:56 PM, anthonym  wrote:
>> Thanks again for the info Alan.  I am still passing the button process
>> until I can get my rectangle and ovals on the canvass.  The program runs
>> and produces a window with the radio buttons on top.  I would like them
>>on
>> the bottom but changes to the row field have no effect.
>>
>> Anybody have any ideas on why I don't see my shape on the canvass?
>
>i doubt i can help, but it might help if you could paste the section
>of the code in relation to what you're explaining and tell us what
>it's doing? some people don't use tkinter so its a bit hard to
>understand what you're saying.
>___
>Tutor maillist  -  Tutor@python.org
>To unsubscribe or change subscription options:
>http://mail.python.org/mailman/listinfo/tutor


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


[Tutor] Calculate hours

2013-01-22 Thread anthonym
Hello All,

I originally wrote this program to calculate and print the employee with the
most hours worked in a week.  I would now like to change this to calculate
and print the hours for all 8 employees in ascending order.

The employees are named employee 0 - 8

Any ideas?

Thanks,
Tony

Code below:



# Create table of hours worked

matrix = [
[2, 4, 3, 4, 5, 8, 8],
[7, 3, 4, 3, 3, 4, 4],
[3, 3, 4, 3, 3, 2, 2],
[9, 3, 4, 7, 3, 4, 1],
[3, 5, 4, 3, 6, 3, 8],
[3, 4, 4, 6, 3, 4, 4],
[3, 7, 4, 8, 3, 8, 4],
[6, 3, 5, 9, 2, 7, 9]]

maxRow = sum(matrix[0])   # Get sum of the first row in maxRow
indexOfMaxRow = 0

for row in  range(1, len(matrix)):
if sum(matrix[row]) > maxRow:
maxRow = sum(matrix[row])
indexOfMaxRow = row

print("Employee 7", indexOfMaxRow, "has worked:  ", maxRow, "hours")



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


Re: [Tutor] Calculate hours

2013-01-22 Thread anthonym
Thanks Dave

I think I would like to keep it simple.  How would I get it to repeat and
print before deleting?

On 1/22/13 7:10 PM, "Dave Angel"  wrote:

>On 01/22/2013 09:52 PM, anthonym wrote:
>> Hello All,
>>
>> I originally wrote this program to calculate and print the employee
>>with the
>> most hours worked in a week.  I would now like to change this to
>>calculate
>> and print the hours for all 8 employees in ascending order.
>>
>> The employees are named employee 0 - 8
>>
>> Any ideas?
>>
>> Thanks,
>> Tony
>>
>> Code below:
>>
>>
>>
>> # Create table of hours worked
>>
>> matrix = [
>>  [2, 4, 3, 4, 5, 8, 8],
>>  [7, 3, 4, 3, 3, 4, 4],
>>  [3, 3, 4, 3, 3, 2, 2],
>>  [9, 3, 4, 7, 3, 4, 1],
>>  [3, 5, 4, 3, 6, 3, 8],
>>  [3, 4, 4, 6, 3, 4, 4],
>>  [3, 7, 4, 8, 3, 8, 4],
>>  [6, 3, 5, 9, 2, 7, 9]]
>>
>> maxRow = sum(matrix[0])   # Get sum of the first row in maxRow
>> indexOfMaxRow = 0
>>
>> for row in  range(1, len(matrix)):
>>  if sum(matrix[row]) > maxRow:
>>  maxRow = sum(matrix[row])
>>  indexOfMaxRow = row
>>
>> print("Employee 7", indexOfMaxRow, "has worked:  ", maxRow, "hours")
>>
>>
>
>What other constraints does your professor make?  For example, are you
>allowed to use sorting with custom key function?
>
>If you want to keep it simple, then invert your logic to find the min
>element.  Then after finding that min element, print it, delete it, and
>repeat the whole thing till the matrix is empty.
>
>
>
>
>-- 
>DaveA
>___
>Tutor maillist  -  Tutor@python.org
>To unsubscribe or change subscription options:
>http://mail.python.org/mailman/listinfo/tutor


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


Re: [Tutor] Calculate hours

2013-01-23 Thread anthonym
Thanks Dave.  I forgot to hit the reply to all last time.  I am going to
try the loop when I get back home and will let you know how I make out.

Also we have done some functions.  Sort and append being among them.  I
try the simplest way first then stream line later.  Probably not the
fastest way.

Tony

On 1/23/13 3:05 PM, "Dave Angel"  wrote:

>You posted this privately to me.  It should have been a message to the
>forum, with tutor@python.org as its to: field.
>
>On 01/23/2013 10:42 AM, anthonym wrote:
>> Should the outside loop have a print command in it so I don't loose that
>> information like I do now?
>>
>
>Yes, the print and the delete are technically in the outer loop.  I was
>thinking in terms of a function, in which case I probably would have
>included those two elements in the function.  But my wording was wrong.
>
>Can you write an attempted solution, and post it to the thread, along
>with whatever difficulty you still see?
>
>Rough pseudo-code
>
>while something-in-list
> find minimum element (this is a loop, similar to the one you
>already wrote)
> print out whatever you need from that element
> delete that element
>
>print out "success"
>
>
>
>-- 
>DaveA


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


[Tutor] lambda

2013-01-25 Thread anthonym
Hello All,

I have the code below that I used to create a simple tic tac toe game for
class.  I am learning Python but have programmed in C+ before so I brought
over a lambda and found that it worked in Python.  Unfortunately I don't
think my classmates will understand the use of lambda here but I am having
are hard time converting that to strictly python.

Let me know if it can be done.

Thanks

from tkinter import *

def ttt(r,c):
global player
if player == 'X':
b[r][c].configure(text = 'X')
player = 'O'
else:
b[r][c].configure(text = 'O')
player = 'X'

root = Tk()

b = [[0, 0, 0],
 [0, 0, 0],
 [0, 0, 0]]

for i in range(3):
for j in range(3):
b[i][j] = Button(font=('Aerial', 56), width=3, bg='yellow',
 command = lambda r=i,c=j: ttt(r,c))
b[i][j].grid(row = i, column = j)

player = 'X'

mainloop()



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


Re: [Tutor] lambda

2013-01-25 Thread anthonym
Thanks Alan.  I prefer the lambda too.  Especially given how much code I
saved.  I forgot about i and j being dynamic and the call function.


On 1/25/13 4:14 PM, "Alan Gauld"  wrote:

>On 25/01/13 23:57, anthonym wrote:
>
>> I don't think my classmates will understand the use of lambda here but I
>> am having are hard time converting that to strictly python.
>
>lambdas are strictly python but they can be easily reanslated into a
>named function as
>
>lambda p: expr
>
>becomes
>
>def f(p):
>return expr
>
>so in your case
>
> >  b[i][j] = Button(font=('Aerial', 56), width=3, bg='yellow',
> >   command = lambda r=i,c=j: ttt(r,c))
>
>becomes
>
>def bCmd(r=i,c=j):
> return ttt(r,c)
>
>b[i][j] = Button(font=('Aerial', 56), width=3, bg='yellow',
>  command = bCmd)
>
>Your problem of course is that you need i and j to be dynamically
>defined so you need to create and call a function that returns a
>function like this
>
>def buttonFunMaker(i,j):
> def func(x=i,y=j):
> return ttt(x,y)
> return func
>
>b[i][j] = Button(font=('Aerial', 56), width=3, bg='yellow',
>  command = buttonFunMaker(i,j))
>
>Personally I prefer the lambda...
>
>HTH
>
>-- 
>Alan G
>Author of the Learn to Program web site
>http://www.alan-g.me.uk/
>
>___
>Tutor maillist  -  Tutor@python.org
>To unsubscribe or change subscription options:
>http://mail.python.org/mailman/listinfo/tutor


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