lllllIllIlllI 178 Veteran Poster

This worked for me:

import wx
import wx.media
import os

class Events():
    def __init__(self,player):
        self.player = player
    
    def pauseFile(self,event):
        self.player.Pause()

class MainFrame(wx.Frame):
    
    def __init__(self):
        
        wx.Frame.__init__(self,None,title = "Media Player")
       
        self.panel = wx.Panel(self)
        self.player = wx.media.MediaCtrl(self.panel)
        self.functions = Events(self.player)
        self.load = wx.Button(self.panel,wx.ID_ANY,"Load File")
        self.load.Bind(wx.EVT_BUTTON,self.loadFile)

        self.play = wx.Button(self.panel,wx.ID_ANY,"Play")
        self.play.Bind(wx.EVT_LEFT_DOWN,self.playFile)

        self.pause = wx.Button(self.panel,wx.ID_ANY,"Pause")
        self.pause.Bind(wx.EVT_LEFT_DOWN,self.functions.pauseFile)

        self.stop = wx.Button(self.panel,wx.ID_ANY,"Stop")
        self.stop.Bind(wx.EVT_LEFT_DOWN,self.stopFile)

        self.slider = wx.Slider(self.panel,wx.ID_ANY,size = (300,-1))
        self.slider.Bind(wx.EVT_SLIDER,self.Seek)

        self.info_name = wx.StaticText(self.panel,wx.ID_ANY,"Name:")
        self.info_length = wx.StaticText(self.panel,wx.ID_ANY,"Time:")
        self.info_pos = wx.StaticText(self.panel,wx.ID_ANY,"Pos:")
       
        sizer = wx.GridBagSizer(5,5)
        sizer.Add(self.slider,(1,1),(1,4))
        sizer.Add(self.load,(5,1))
        sizer.Add(self.play,(5,2))
        sizer.Add(self.pause,(5,3))
        sizer.Add(self.stop,(5,4))
        sizer.Add(self.info_name,(2,1),(1,4))
        sizer.Add(self.info_length,(3,1),(1,4))
        sizer.Add(self.info_pos,(4,1),(1,4))
        self.panel.SetSizer(sizer)

        self.Show()
       
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.onTimer)
        self.timer.Start(100)
       
        self.panel.SetInitialSize()
        self.SetInitialSize()

    def onTimer(self,event):
        current = self.player.Tell()
        self.info_pos.SetLabel("Pos: %i seconds" % (int(current)/1000))
        self.slider.SetValue(current)
      
    def Seek(self,event):
      
        self.player.Seek(self.slider.GetValue())
     
       
    def loadFile(self,event):
       
        msg = wx.FileDialog(self, message = "Open a media file",
                               style = wx.OPEN,
                               wildcard = "Media Files|*.wma;*.mp3;*.avi")
        if msg.ShowModal() == wx.ID_OK:
            path = msg.GetPath()
            self.path = path
           
            if not self.player.Load(path):
                wx.MessageBox("Unable to load this file, it is in the wrong format")
            else:
                self.player.Play()
               
               
               
               
    def playFile(self,event):
        print self.path
        self.player.Play()
        print self.player.Play()
        self.slider.SetRange(0, self.player.Length())
        self.info_length.SetLabel('length: %d seconds' % (self.player.Length()/1000))
        self.info_name.SetLabel("Name: %s" % (os.path.split(self.path)[1]))
        self.panel.SetInitialSize()
        self.SetInitialSize()
       
    #def pauseFile(self,event):
       # self.player.Pause()
       
    def stopFile(self,event):
        self.player.Stop()
   
       
app = wx.App(False)
frame = MainFrame()
app.MainLoop()

Oh and there is a little bug i forgot about in the code. Just you have to remember at some point to add self.player to the sizer otherwise videos dont play.

lllllIllIlllI 178 Veteran Poster

Try open Ctrl+Alt+Del and going to the processes tab. Then look for any processes called "python.exe" or "pythonw.exe" then end all of those. If you have any python windows open they will shut so close everything beforehand.
Then once all processes called "python.exe" or "pythonw.exe" have ended it should work. This has worked for me before.

lllllIllIlllI 178 Veteran Poster

Ah, your problem is the following.
You have put in your raw_input() to open the file e:. The problem with that is that you are asking it to open a full directory, not just a file. I had this same problem instead with writing files. You need to specify an actual FILE to open, not a folder or anything else.
So if you put in e:\test_file.txt into the raw_input() and there was a text file called text_file then your program would work.

lllllIllIlllI 178 Veteran Poster

I got this problem once. I fixed it my (in windows XP) by opening the task manager and ending any processes of python. I found that some of my previous programs, usually the wxPython ones, were excellent at leaving behind processes especially if they didnt end properly. I find that fixes most of my problems with things not connecting.

Hope that helps.

lllllIllIlllI 178 Veteran Poster

Just a word of advice about your if in your code. That is statement will almost always execute unless start is equal to zero. The reason is, even though saying OR something else. You cant apply the same operator to each. So you would have to go:

if start >0 or end > 0:
    #do stuff
lllllIllIlllI 178 Veteran Poster

I have noticed that whenever i run it onSetPath() never seems to ever get run. Would this be a problem, or is it just me that this is happening to?

lllllIllIlllI 178 Veteran Poster

Where can we download all of the bitmaps for the buttons for it?

lllllIllIlllI 178 Veteran Poster

Could you test how many pixels are red? Because a circle will require many more pixels to draw then a line.
I know when getting colours i used something like:

import Image
im = image.open("myPic.jpg")
for pixel in im.getdata():
    #check if it is red
lllllIllIlllI 178 Veteran Poster

Just make sure that both things you want are methods. I think you will want to do something like:

import wx #if using wxPython
import thread

class Frame(wx.Frame):
    #all the code stuff

def image(path):
    #image stuff

# then you go:
app = wx.App(0)
f = Frame()
thread.start_new_thread(app.MainLoop,())
thread.start_new_thread(image,(path))
shadwickman commented: Threading idea/example was a HUGE help! +2
lllllIllIlllI 178 Veteran Poster

Well python only does one thing at a time so perhaps while the images are being done it dosent do the GUI. A way around this is to use the thread module.

Threading is useful if you want two things to be able to happen at once.

lllllIllIlllI 178 Veteran Poster

Woops, should have called them paramaters. If you have any that are needed then you put them as the second paramater but if there are no paramaters needed for the MeWin method then you dont need to have a second paramater.
Note that you dont put parentheses after the self.MeWin the the wx.CallAfter.

Stefano Mtangoo commented: Brilliant Boy! +2
lllllIllIlllI 178 Veteran Poster

you would call wx.CallAfter(self.MeWin,ARGUMENTS) and then that will call the function after the YouWin function is done.

lllllIllIlllI 178 Veteran Poster

It is used with events in wxPython. If you have a button lets say that when you press it pops up a dialog box. But there is also something else going on and you want the method that makes that dialog box to be called After the event function is done.

So here is the example from http://wiki.wxpython.org/CallAfter

import threading,wx

ID_RUN=101
ID_RUN2=102

class MyFrame(wx.Frame):
    def __init__(self, parent, ID, title):
        wx.Frame.__init__(self, parent, ID, title)
        panel = wx.Panel(self, -1)
        mainSizer=wx.BoxSizer(wx.HORIZONTAL)
        mainSizer.Add(wx.Button(panel, ID_RUN, "Click me"))
        mainSizer.Add(wx.Button(panel, ID_RUN2, "Click me too"))
        panel.SetSizer(mainSizer)
        mainSizer.Fit(self)
        wx.EVT_BUTTON(self, ID_RUN, self.onRun)
        wx.EVT_BUTTON(self, ID_RUN2, self.onRun2)

    def onRun(self,event):
        print "Clicky!"
        wx.CallAfter(self.AfterRun, "I don't appear until after OnRun exits")
        s=raw_input("Enter something:")
        print s

    def onRun2(self,event):
        t=threading.Thread(target=self.__run)
        t.start()

    def __run(self):
        wx.CallAfter(self.AfterRun, "I appear immediately (event handler\n"+ \
                        "exited when OnRun2 finished)")
        s=raw_input("Enter something in this thread:")
        print s

    def AfterRun(self,msg):
        dlg=wx.MessageDialog(self, msg, "Called after", wx.OK|wx.ICON_INFORMATION)
        dlg.ShowModal()
        dlg.Destroy()


class MyApp(wx.App):
    def OnInit(self):
        frame = MyFrame(None, -1, "CallAfter demo")
        frame.Show(True)
        frame.Centre()
        return True

app = MyApp(0)
app.MainLoop()
lllllIllIlllI 178 Veteran Poster

what does "DictionaryE.txt" look like? I would like to try and run it but i get an error because i have no "DictionaryE.txt" file.

lllllIllIlllI 178 Veteran Poster

you might have an issue with variables not being global. Try putting this at the start of your program.

global file_in
global file_size
lllllIllIlllI 178 Veteran Poster

see the indentation of line 14 and line 13 and line 12 and line 11 do not match the indentation needed to set them all inside the function. You just need to re-write this in a python editor and it should auto-indent it for you.

it should be

def random_word ():
    while True:
        offset =randint (0 ,file_size )
        file_in .seek (offset)
        L=file_in .read (25 ).split ("\r\n")
        if len (L)>2 :
            return L [1]
lllllIllIlllI 178 Veteran Poster

That will mean that the return is not being properly recognised as inside of the function. This could be an indenting problem or just accidentally having a return statement somewhere else in your program that isnt a function.

here is something that will raise that error

def add(x,y):
    z = x+y
return z

this will work though

def add(x,y):
    z = x+y
    return z
lllllIllIlllI 178 Veteran Poster

you use a range.

for variable in range(n/2):
    #do stuff
lllllIllIlllI 178 Veteran Poster

Could we have an example of what stock.txt has in it please?

lllllIllIlllI 178 Veteran Poster

This is not very hard if you use the random module.

import random

words = ['hello','python','monday','tuesday','friday','ice']
choice = random.choice(words)

print "Guess the word!"
guess = raw_input()
while guess.lower() != choice:
    print "sorry, not correct"
    guess = raw_input()
print "well dont that was it!"
lllllIllIlllI 178 Veteran Poster

I think you could also use XML to save it. If you are using wxPython then there is an XML module you can load that makes it nice and easy to load.

The module is wx.xrc, it can load a panel, a frame or any other kind of object. Its great to have a fiddle with.

lllllIllIlllI 178 Veteran Poster

If you want to do that to an int and you just want to add it on the end then you could go:

>>> adam = 123
>>> john = 456
>>> #if you just add them together they don't add on to the end, 
>>> print adam+john
579
>>> print str(adam)+str(john)
123456
>>> # and then you can turn that back into an int using the int() function
lllllIllIlllI 178 Veteran Poster

Python would be easy to do this in. You could use a mixture of the random module as well as the String module and combining them together to get something that does it for you. In fact i will show you how i would do it if i was making a program that made a random string.

import random
import string

r = input("How long should the string be?")
s = ""
for number in range(r):
    s += string.lowercase[random.randint(0,26)]
print s
lllllIllIlllI 178 Veteran Poster

I find functions still a great thing to have if you want to have something unrelated to either class. Personally i like to only have related functions in a class and if i want a function and there is no specific class that it fits into then i do not put it in a class and leave it out.

lllllIllIlllI 178 Veteran Poster

You could use list comprehension as well

binary = raw_input()
if False in [f == '0' or f == '1' for f in binary]:
	print "Not Binary"
else:
	print "Is Binary"
lllllIllIlllI 178 Veteran Poster

Okay, you have to use your TextCtrl and then you have use something like:

t = wx.TextCtrl(arguments)
#... later
t.GetValue()

the GetValue() method fetches the current value of the textCtrl

lllllIllIlllI 178 Veteran Poster

You have a lot of errors your code. First of all let me say that you can really only use self when you are in a class. A lot of your code in your program should be in the __init__ method.

Also in you Add method you have return x as the first thing you say. This will stop the rest of the code from running. Instead it should be like:

def Add(self,event):
        
        y=input("")
        print x + y

Just dont worry about returning anything. You need not.

For your events you want wx.EVT_LEFT_DOWN to stimulate on a mouse click.

If fiddled around and this is what i got. It isnt perfect but it will help you along:

#!/usr/bin/python

# gridsizer.py

import wx

class GridSizer(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(300, 250))
        menubar = wx.MenuBar()
        file = wx.Menu()
        file.Append(0, '&Quit', 'Exit Calculator')
        menubar.Append(file, '&File')
        self.SetMenuBar(menubar)

        sizer = wx.BoxSizer(wx.VERTICAL)
        self.display = wx.TextCtrl(self, -1, '',  style=wx.TE_RIGHT)
        sizer.Add(self.display, 0, wx.EXPAND | wx.TOP | wx.BOTTOM, 4)
        gs = wx.GridSizer(4, 4, 3, 3)
        self.e_but = wx.Button(self, 3, '=')
        self.e_but.Bind(wx.EVT_LEFT_DOWN, self.On)
        self.a_but = wx.Button(self, 2, '+')
        self.a_but.Bind(wx.EVT_LEFT_DOWN,self.Add)
    
        gs.AddMany([(self.e_but, 0, wx.EXPAND),(self.a_but, 0, wx.EXPAND)])
                       
                     
        sizer.Add(gs, 1, wx.EXPAND)
        self.SetSizer(sizer)
        self.Centre()
        self.Show(True)
    def On(self, event):
        x=input("")
        
    def Add(self,event):
        
        y=input("")
        print x + y



     
   
   
app = wx.App(False)
GridSizer(None, -1, 'GridSizer')
app.MainLoop()

Note that your program is not getting input from the GUI the input is still coming from the python console.

lllllIllIlllI 178 Veteran Poster

Okay. Im glad it works. Well done on fixing it yourself.

lllllIllIlllI 178 Veteran Poster

That means somewhere in your code. Perhaps in a bit that i cant see. There is a bit that makes balance not an int any more. So try and look for places where balance is changed.

lllllIllIlllI 178 Veteran Poster

Well this is working for me:

def process(balance):
    trans_type= raw_input("Enter Deposit(d), withdrawal(w), print(p), or quit(q):")
    amount=input("Enter the amount:")
    if amount<0:
        print"You've entered an invalid number."
    if trans_type=="d":
        balance= balance+amount
        print"Your current balance is", balance
    elif trans_type=="w":
        balance= balance-amount
        print"Your current balance is", balance
    return balance



def main():
    balance=5000
    restart=raw_input("Would you like to complete a transaction? (y/n):")
    while "y"==restart:
        balance = process(balance)



main()

I am doing all the things that you say you cant. I took out the changing it to an int with the int() function because it was doing nothing. Perhaps that did something. Try copy-paste my code.. see if it works on your computer.

lllllIllIlllI 178 Veteran Poster

Can you please give us examples of numbers which stuff up the program and the FULL error statement because that will help even more. Also tell us wether you are doing a withdrawal, deposit or something else at that time.

Oh and please wrap your code in code tags. I will do it for you this time

def process(balance):
    trans_type= raw_input("Enter Deposit(d), withdrawal(w), print(p), or quit(q):")
    amount=input("Enter the amount:")
    if amount<0:
        print"You've entered an invalid number."
    if trans_type=="d":
        balance=int(balance)+int(amount)
        print"Your current balance is", balance
    elif trans_type=="w":
        balance=int(balance)-int(amount)
        print"Your current balance is", balance
    return balance



def main():
    balance=5000
    restart=raw_input("Would you like to complete a transaction? (y/n):")
    while "y"==restart:
        balance = process(balance)



main()

Also notice at the while "y" == restart bit? That will never stop executing. Replace all of main with another main method like this:

def main()
    restart = raw_input("Would you like to complete a transaction? (y/n):")
    balance = 5000
    while restart == "y":
        balance = process(balance)
        restart = raw_input("Would you like to complete another transaction? (y/n):")
lllllIllIlllI 178 Veteran Poster

Congradulations, you have done very well.
:)

lllllIllIlllI 178 Veteran Poster

try using wx.EVT_TEXT and bind it to the Rich Text control.

lllllIllIlllI 178 Veteran Poster

How are you doing it?
I mean have you changed your code now so it goes:

module.monster
#rather then before
monster

Because unless you go

from module import *

You have to tell the program what module it is in. The second method imports all that is inside the module so you wouldn't have to change a thing.

lllllIllIlllI 178 Veteran Poster

Ah thats an easy one. Look at the class demon. Okay, notice in the __init__ method you call monster.__init__? Well remember to close those brackets.

That should fix it.

lllllIllIlllI 178 Veteran Poster

Well the problem is that in your text file you may have an empty line and therefore splitting it will give you a list with nothing in it and therefore if you try and acces the variable at location 0 there is nothing there.
A list index out of range means that the list is not large enough to have a value stored at that location.

Hope that clears it up.

lllllIllIlllI 178 Veteran Poster

Maybe instead of randrange you could have a list of possible hit vaules. This way you could have zero in there only a few time and higher numbers in more.

Im glad to hear that it is working.

Oh also maybe another way to do it would to have a variable that is set at the max number of hits with no value and then every time there is a hit of zero the counter goes down, and then if the counter is at zero and the randrange gets zero then you could make it re-do it.

lllllIllIlllI 178 Veteran Poster

May i just say well don on the new code. It is a lot better then the old code and it is great you learnt from your old code and this new stuff is a lot better.
Well Done!

lllllIllIlllI 178 Veteran Poster

Planning is probably one of the best things you can do in a programming project. A lot of people when given a project will start typing straight away, i find that if i plan my code. Determine what features i want and work out how i am going to get them then i find the actual coding part a lot easier.

Im glad you liked it.

lllllIllIlllI 178 Veteran Poster

Okay, well if you have any question just post them. It is a good idea to also test code as you go. That way you dont end up with massive errors that you need to re-write your code for.

But good luck!

lllllIllIlllI 178 Veteran Poster

Okay, well at the moment there are so many errors that i really cant begin to start. I have spent about 20 minutes trying to get your code to work but there are lots of fundamental things you need to do. I will outline the changes you need to do so it will be easier for all of us to help you.

ONE:
Change all _init_ methods in ALL of your classes to __init__ methods, you need those extra underscores for it to run, notice you will get a few errors when you try and instantise them, thats fine, just remove the arguments. Look at the exception carefully, it can tell you a lot.

TWO

for self.damage in axe() and sword():

That just dosent work. You cant iterate over things like that, your going to have to work out something else.

THREE

if self.attack > troll.defense or orc.defense:

This dosent work either. Python checks if self.attack is more then troll.defence. If False then it checks the other requirement which is orc.defence but seeing nothing is being compared with it as long as it is not 0 then it is True and the if runs. Also you cant use classes like that. Instantise all classes!

FOUR
Think through things logically and fiddle around, try things for yourself and see how things go. If you are stumped and cant think of anything, google it. Then ask us if nothing works. We cant do everything.

lllllIllIlllI 178 Veteran Poster

Ah wait!! I found something wrong in your code!! Wow im amazed i didnt spot it before! You know your _init_ statement in player only has one _ when it needs two __. Replace _init_ with this __init__ . That should solve some major issues!

lllllIllIlllI 178 Veteran Poster

I noticed in your previous code for your attack function you did not have self in the arguments. Is that how it is now? If not make sure self is in the arguments/parameters, whatever they are called :P.

Perhaps maybe post an update of your code as it is now.

lllllIllIlllI 178 Veteran Poster

Okay, i think your problem here is that you are giving your class player all the attributes of monster but you forget to initialise monster. Try adding

Monster.__init__()

to your player class __init__ code. Then you can refer to anything from monster as self. So it would be self.defense rather then monster. Might cause some issues with conficting variable names, just fiddle around a bit with it.

lllllIllIlllI 178 Veteran Poster

Well i think that you should declare things like the players attack in the __init__ statement of player and make sure that their variable names dont point to anything else such as a method or an already made variable. So for example you could put your attack in a variable called self.attack_points.

That way there are no other variables or methods called the same things as well as you would just replace the old self.weapon.attack with self.attack_points and everything would hopefully work a lot better.

lllllIllIlllI 178 Veteran Poster

That is because when you go self.weapon.attack it logically goes, well self.weapon is a function in the class and then it looks for the variable attack as part of the method weapon. This is a problem and you should change this.

I noticed a lot of the things that you are using in your code have not actually been declared. Make sure to declare all of your variables.

lllllIllIlllI 178 Veteran Poster

Again you need to create an instance of player so somewhere put this:

p = player()
#then when you want to attack
p.attack()

hope that helps.

lllllIllIlllI 178 Veteran Poster

Is this what your talking about?

import random
count = input("How long shall the sequence be?")
seq = ''.join([random.choice('AGTC') for x in range(count)])
print "Length:",count
print ">",seq
lllllIllIlllI 178 Veteran Poster

Sorry wait, that only solves half of the problem. The thing i only just spotted was that you go Monster.__init__. That then means that all of Monsters methods will now be stored in self. So you can call self.getAttributesInfo But the problem with that is there are two methods called self.getAttibutesInfo One is in the Orc and Troll classes and the other is in the Monster class so then we have to change the name of one of them to make sure that the program does not call the method over and over and over.

I fiddled with the code and here is what i got:

#!/usr/bin/env python

import random

choice = 0

class weapon(object):
    def _init_(self, attack, defense):
        self.attack = self.attack
        defense = self.defense

    def getAttributesInfo(self):
        attributes = ['Attack:' + self.attack,
        'Defense:' + self.defense]
        return ' '.join(attributes)

class sword(weapon):
    def _init_(self):
        wepon._init_(self, random.randrange(1,3), random.randrange(1,3))

    def getAttributesInfo(self):
        weapon.getAttributesInfo()

class axe(weapon):
    def _init_(self):
        weapon._init_(self, random.randrange(1,5), random.randrange(1,5))
        print 'This Axe\'s Attributes are:'
        print 'Attack:' + self.attack
        print 'Defense:' + self.defense

    def getAttributesInfo(self):
        weapon.getAttributesInfo()
        
class player():
    def _init_(self):
        self.health = 10
        self.defense = 4
        self.base_attack = 3
        self.attacking = 1

    def weapon(self):
        self.weapon = random.range(1,2)
        if self.weapon == 1:
            self.weapon = sword()
            print "you got a sword."
        elif self.weapon == 2:
            self.weapon = axe()
            print "you got an axe."

    def attack(Monster):
        if not self.attacking == 1:
            self.attacking = 1
        if self.weapon.attack > monster.defense:
            hit = True
            monster.health = monster.health - self.weapon.damage
            print 'You hit him for ' + self.damage + 'damage!' …
lllllIllIlllI 178 Veteran Poster

In your code you go:

Monster.getAttributesInfo()
#and
weapon.getAttributesInfo()

The problem with this is that you are not instanciating your class. First creat an instance of the class and then you can call methods then. Otherwise you will keep getting this error.